TextBox vertical alignment in Windows Store App - c#

I'm trying to do something that seems quite simple but that I haven't been able to do so far : Vertically align the text in a TextBox in Windows Store App (XAML).
VerticalContentAlignment doesn't work (although it works well in WPF). I've even tried to extract the template and change it. Still couldn't do it.
Does anyone know how to do it ?

Unfortunately there is no simple way. However if your TextBox has a fixed size then you can adjust the Padding Property in a custom style, it can be done dynamically too, but it is a bit more work.
<Style TargetType="TextBox" x:Key="CustomTextBoxStyle">
<Setter Property="Padding"
Value="25" />
</Style>
<TextBox Text="TestText"
Height="80"
Style="{StaticResource CustomTextBoxStyle}" />

Also you can try this one also...
<Grid>
<TextBox Text="Centered Text" TextAlignment="Center" VerticalAlignment="Center"/></Grid>

Related

WPF and FontAwesome problem with hyphenated icons

I am using FontAwesome in my WPF app for icons.
Everything works as expected with single name icons, but whenever I try to use a hyphenated icon name, the font does not understand this, and either does not draw anything, or it draws the two icons (left and right of the hyphen) individually (if they exist).
This draws the user icon. Great.
<!--FontAwesome Font applied in style-->
<TextBlock Style="{DynamicResource mainButtonImage}" Margin="5" Text="user"></TextBlock>
This, draws the User icon, a "-" and a circle icon. It should be the "user-circle" icon.
<TextBlock Style="{DynamicResource mainButtonImage}" Margin="5" Text="user-circle"></TextBlock>
It should be drawing this: https://fontawesome.com/icons/user-circle?style=regular
When I test in notepad on my system, it works as expected.
Any ideas?
I implemented the "user-circle" icon in two ways below:
Method 1:
1.You can install FontAwesome.WPF in project's Package NuGet Manager.
2.Import xmlns:fa="http://schemas.fontawesome.io/icons/" into your XAML code.
3.Use it into your TextBlock like this:
<fa:FontAwesome Icon="UserCircle" FontSize="100"></fa:FontAwesome>
Method 2:
1.Download font awesome at fontawesome.
2.Unzip the file and copy font to the project as a resource. The path is \Font\fa-regular-400.ttf.
3.Add FontAwesome style in Window.Resources:
<Window.Resources>
<Style x:Key="FontAwesome">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Font/#Font Awesome 5 Free Regular" />
</Style>
</Window.Resources>
4.Use style in TextBlock and write the Unicode code of user-circle in Text like this:
<TextBlock Text="" Style="{DynamicResource FontAwesome}" />
Maybe you can refer to these two methods to implement your program.

Hiding AutoSuggestBox in NavigationView

I have a NavigationView control with an AutoSearchBox displayed:
<NavigationView Style="{StaticResource CompactNavigationViewStyle}"
x:Name="NavigationView" OpenPaneLength="280"
VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"
AlwaysShowHeader="False">
<NavigationView.AutoSuggestBox>
<AutoSuggestBox PlaceholderText="Search" QueryIcon="Find" Width="235"
x:Name="SearchTxt" QuerySubmitted="OnSearch" />
</NavigationView.AutoSuggestBox>
For some page I don't want to show the Search (i.e SettingPage), so on NavigationView ItemInvoked event I added this code:
private void NavigationView_ItemInvoked(NavigationView sender,
NavigationViewItemInvokedEventArgs args)
{
if (!args.IsSettingsInvoked)
{
string tag = (args.InvokedItem as string);
switch(tag)
{
case "settingpage":
case "exportpage":
SearchTxt.Visibility = Visibility.Collapsed;
break;
default:
SearchTxt.Visibility = Visibility.Visible;
break;
}
// Code to load new page to Frame here
}
}
The AutoSuggestBox is hidden from NavigationView, but the Search icon still displayed on Compact mode, how to hide this icon too?
Unfortunately, what you are looking for does not appear to be possible with the NavigationView control. This is relatively new control, however, so Microsoft may be updating it in the future.
For the technical reason, setting a value to the AutoSuggestBox property does more than just add an AutoSuggestBox to the NavigationView; it also changes some other internals of the NavigationView (such as the one specifying showing that search icon when the pane is collapsed).
The AutoSuggestBox property is an optional property, and designed to contain a search box "to allow for app-level search". This suggests that it is designed to be always visible across the entire app when present (although I could see a reasonable argument being made for having it disabled on certain pages). But simply, it looks like this is a use case the control is not designed for.
As for some ideas for work-arounds:
Option 1
The one place you can freely put whatever content you want is the NavigationView.Footer. You can implement something like the above code, and then adjust the StackPanel's visibility property.
The main downside to this option, of course, is that the footer is stuck at the bottom, which may be an odd place to put a search bar. You also will need to give the StackPanel some visual styling to mimic the hover and click effects on the rest of the NavigationView.
<StackPanel Orientation="Horizontal"
Margin="10">
<TextBlock Style="{StaticResource HamburgerMenuIconStyle}"
Text=""></TextBlock>
<TextBlock Style="{StaticResource HamburgerMenuItemTextStyle}"
Text="Home"></TextBlock>
</StackPanel>
And then the supporting styles for the Page.Resources, so that the above bindings work (you may need to play around with the numbers for the margins and font sizes, but this looked good for me):
<Page.Resources>
<Style x:Key="HamburgerMenuIconStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"></Setter>
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Margin" Value="5,0,0,0"></Setter>
</Style>
<Style x:Key="HamburgerMenuItemTextStyle" TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Margin" Value="15,0,20,0"></Setter>
<Setter Property="FontSize" Value="16"></Setter>
</Style>
</Page.Resources>
Option 2
You can implement your own NavigationView, using a SplitView. This is actually not too difficult, and there is even a fantastic tutorial in the following link (which I have used for several personal projects): https://mva.microsoft.com/en-US/training-courses/windows-10-development-for-absolute-beginners-14541?l=4DLgEZ0qB_5705244527. Specifically see Video #22, if the link doesn't open that one.
Implementing your own works well enough for visual looks, and for navigational functionality. Unfortunately, however, the more advanced features in the built-in NavigationView control are considerably more time-consuming to implement, such as the built-in "Back" navigation support, and the built-in fluent design styling. You can wrap this into a UserControl or a custom Control, so that you can reuse it in other projects, but the initial time investment will still be high.
It is worth noting that the requirements you are imposing on your users by using a NavigationView in the first place should be considered (if you have not done so already):
The NavigationView was introduced in the Fall Creator's Update, so any users must have a version newer than that. Somewhere around 90% of users who have Windows 10 do, so this is pretty safe for most people.
The Back navigation functionality of the NavigationView was introduced even more recently, specifically in v10.0.17110.0. This is still in Windows Insider, and not generally released yet (I believe), so this functionality specifically may not be a good choice for a larger audience yet. The reason I point this out, is that to reach a larger audience, you would need to implement the back functionality yourself anyway, so the barrier to writing your own NavigationView may not be as high as it seems.
Hope that helps!
<NavigationViewItem
x:Name="NaviSearchItem"
Icon="Find"
Visibility="Collapsed" />
<NavigationViewItem x:Name="NaviSearchBarItem">
<AutoSuggestBox
x:Name="NaviSearchBar"
PlaceholderText="Search"
QueryIcon="Find" />
</NavigationViewItem>
You can surround the AutoSuggestBox with NavigationViewItem and place the Search Button above it. Then you need to set the visibility of NaviSearchBarItem instead of the AutoSuggestBox. This hides the AutoSuggestBox perfectly.
However, there is a tiny issue with this solution. When you click on the NaviSearchBarItem to open the Pane, the animation of tab (a blue pipe that indicates the selected item) will still slide to the NaviSearchBarItem, and then it disappears. The ideal solution should be that the blue indicator still remain on the original item. I don't know how to fix this.

WPF Template Binding with more than 1 content

So, I have this Window. On it, I'm creating a list of TextBlocks and TextBoxes in pairs. When you click on either, they will put a number in the corresponding TextBox, and set some values in the background. This all works well now.
I have the following XAML to create a custom Checkbox (as it has the behavior I'd like to use for this). My problem is that I want to bind different content into both the TextBlock and TextBox. For the TextBlock, I bound to the Content property, but I can't find a suitable option to satisfy the second binding. I considered placing it in the tag, but this didn't feel right, and in any case, I'm already binding an index value I require into there.
<Style x:Key="CustomCHK" TargetType="{x:Type CheckBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<DockPanel LastChildFill="True">
<TextBox DockPanel.Dock="Right" Width="50" Height="30" />
<TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Feels like there should a simple solution to this, but I'm just trying to decide what's best. Do I create a custom checkbox class and just add a couple properties?
As always, I appreciate any direction you can offer me.
Unfortunately, there is no straightforward way to do this. I can see just two (somewhat flawed) workarounds:
Subclass CheckBox to suit your needs, and add the additional content properties that you need. The advantage is that this will fully enable your IDE's programming help for setting the content properties, and those properties will be typesafe. The downside is that your will need to add C# code for the sole purpose of declaring the additional content properties (i.e. without adding any "behavioral logic"), which somehow seems to conflict with a "clean" XAML-only for presentation approach.
You could try passing an array to the Content property, and then place several ContentPresenter instances in your control template, each of which bind to another item in the array. Binding property paths should support indexed access, though your code may become a bit verbose, as arrays in XAML have to be written explicitly by using the x:Array markup extension.

Attach attribute to Scrollviewer of Textbox

I need to sync two scroll viewers in WPF, one of which is part of the TextBox item.
I intend on using the method described on CodeProject here:
However, this requires that I can attach attributes to both scrollviewers. One of the scrollviewers is the scrollviewer that comes as part of a textbox.
Code at the moment:
<ScrollViewer Core:ScrollSynchronizer.VerticalScrollGroup="V1">
<UIComponents:LineNumberBox
x:Name="LineBox"
VisibleLines="{Binding ElementName=CodeBox, Path=(UIComponents:VisibleLinesBinder.VisibleLines)}"
Padding="2,10,2,0"
FontSize="14"
Grid.Column="0"
/>
</ScrollViewer>
<UIComponents:SourceCodeBox
x:Name="CodeBox"
Padding="10"
FontSize="14"
Grid.Column="1"
UIComponents:VisibleLinesBinder.ObserveVisibleLines="True"
Core:ScrollSynchronizer.VerticalScrollGroup="V1"
/>
A UIComponents:SourceCodeBox is just a wrapper around a normal WPF Textbox.
Obviously the Core:ScrollSynchronizer.VerticalScrollGroup="V1" on the SourceCodeBox doesnt work? So how would I attach that attribute to the ScrollViewer within it? C# or XAML methods are both fine.
In case it makes any difference this is part of a User Control I am developing.
put the attatched behavior in a style like this:
<UIComponents:SourceCodeBox
x:Name="CodeBox"
Padding="10"
FontSize="14"
Grid.Column="1"
UIComponents:VisibleLinesBinder.ObserveVisibleLines="True"
Core:ScrollSynchronizer.VerticalScrollGroup="V1">
<UIComponents:SourceCodeBox.Resources>
<Style TargetType="ScrollViewer">
<Setter Property="Core:ScrollSynchronizer.VerticalScrollGroup" Value="V1"/>
</Style>
</UIComponents:SourceCodeBox.Resources>
</UIComponents:SourceCodeBox>

"ReorderListBoxItem must have a DragHandle ContentPresenter part." issue

We are working on Windows Phone 8 app and try to use the reorder list box control. For previous version of our app it's working. But now, when i bind the collection to the reorder list box, i've got exception "System.InvalidOperationException"
In details, i see this message.
System.InvalidOperationException: ReorderListBoxItem must have a DragHandleContentPresenter part.
at ReorderListBoxDemo.ReorderListBoxItem.OnApplyTemplate()}
If anybody see such problem or know anything about it - please help. We use the MVVM light and bind data through ViewModel. But raw binding is not working at all. Sorry for my Russian English. :)
The control requires a Setter with Property="DragHandleTemplate". So simply copy the base ItemContainerStyle from the Codeplex page of the control and extend it as required:
<rlb:ReorderListBox.ItemContainerStyle>
<Style
TargetType="rlb:ReorderListBoxItem">
<Setter
Property="DragHandleTemplate">
<Setter.Value>
<DataTemplate>
<Canvas
Width="52"
Height="48"
Background="Transparent">
<Polygon
Fill="Gray"
Points="16,20 4,20 22,4 22,44 16,44" />
<Polygon
Fill="Gray"
Points="32,28 44,28 26,44 26,4 32,4" />
</Canvas>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</rlb:ReorderListBox.ItemContainerStyle>

Categories

Resources