Display multiline DynamicResource Label - c#

I wish to add a Label in WPF that displays string from two different DynamicResources.
I want each DynamicResource to be on a new line.
My existing code is:
<Label x:Name="MyTextDisplay"
Grid.Row="3"
Grid.ColumnSpan="2"
Background="Red"
BorderBrush="Blue"
BorderThickness="1"
Margin="2, 2, 2, 2">
<TextBlock TextWrapping="Wrap" Text="{DynamicResource MyTextLine1}" Grid.ColumnSpan="2"/>
</Label>
I have another DynamicResource called MyTextLine2 that I want to display below MyTextLine1 but in the same Label.
How can I do this?
I have looked at these examples here but they dont display on new lines: How to bind multiple values to a single WPF TextBlock?

I faced the same problem and finally I found a solution.
Just use \r\n linebreak instead of just \n.
So, your resourse must look like:
<system:String x:Key="MyText" xml:space="preserve">Line 1
Line 2</system:String>
I realy don't know why this notation must be used only for dynamic resources, but it works for me

Related

How do I reduce the line spacing in XCeed WPF Extended Toolkit Richtextbox

I am using the WPF Extended Toolkit Richtextbox to display simple text. But the lines are widely separated rather than one after the other. Indentation appears to be working OK. It's just that 5 lines of text takes up a lot of space. I tried setting Margin="0." I cannot find a property to set such as "Line Spacing" to set.
<tools:RichTextBox Grid.Row="3"
Grid.Column="1"
Text="{Binding Procedure}"
Margin="0"
ScrollViewer.VerticalScrollBarVisibility="Auto"
BorderBrush="Gray"
BorderThickness="2"
Padding="0"
AcceptsReturn="True"
AcceptsTab="True"
AllowDrop="True"
AutoWordSelection="True"
>
<tools:RichTextBox.TextFormatter>
<tools:PlainTextFormatter/>
</tools:RichTextBox.TextFormatter>
</tools:RichTextBox>
The Document property can not be set with a binding.
Line spacing cannot be formatted using the rtf header with the Text attribute in the extended wpf richtextbox. Use the Block.LineHeight attribute instead. It has to be about 130-150% of the font size to work.
<xctk:RichTextBox Block.LineHeight="20"/>

Formatted Textboxes for UWP

I was wondering if anybody had any recommendations on how I could format a textbox for time inputs(hh:mm:ss) as well as Coordinates in Degrees,Minutes, Seconds (dd°mm'ss"). I am well aware that you can set the format of a string by the String.Format() method.... this does give me the desired layout in the textbox, but I want a control that has the appropriate symbols locked in and will autotab over those symbols as the user gives their inputs.
I have been searching for a way to do this in C#/UWP for a while with no luck, if anybody has any suggestions or could point me in the right direction it would be greatly appreciated.
The UWP Community Toolkit has some very good controls which are easy to integrate and one of them is the TextBox Mask control which is ideal for your scenario.
You can also try out the TextBoxRegex control which is not quiet what you want but is indeed something you should check out before proceeding.
Please reach out in case you are having difficulty in integrating the control for your specific requirement.
Optionally, you can download the sample app for the UWP toolkit to know about all the available controls /Tools. (UWP Community Toolkit Sample App)
EDIT 1:
As you might have noticed, the default behaviour for tab is that it sets the focus to the next control. Thus, having a single control will not be ideal since on press of tab, your textbox would lose focus and you would need to update the text and set the focus back to your textbox.
A relatively simpler way would be the following:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1">
<StackPanel.BorderBrush>
<SolidColorBrush Color="{ThemeResource SystemBaseHighColor}"/>
</StackPanel.BorderBrush>
<TextBox x:Name="hour" MaxLength="2" BorderBrush="{x:Null}" VerticalAlignment="Center" HorizontalAlignment="Center" LostFocus="hour_LostFocus"></TextBox>
<TextBlock Text=":" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="minute" MaxLength="2" BorderBrush="{x:Null}" HorizontalAlignment="Center" VerticalAlignment="Center" LostFocus="minute_LostFocus"></TextBox>
<TextBlock Text=":" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="second" MaxLength="2" BorderBrush="{x:Null}" HorizontalAlignment="Center" VerticalAlignment="Center" LostFocus="second_LostFocus"></TextBox>
</StackPanel>
This is basically your custom build control/usercontrol. So that when user tabs out it moves to the next textbox inside the stackpanel and so on.
In your hour_LostFocus event you can validate the textbox's current value and append 0 and do other validations .
private void hour_LostFocus(object sender, RoutedEventArgs e)
{
string val = (sender as TextBox).Text;
Regex regex = new Regex(#"^([0[0-9]|1[0-9]|2[0-3])$");
Match match = regex.Match(val);
if (!match.Success)
{
//append 0 and other validations
}
}
The same applies for minute and seconds .. just that the regex would be different in these cases.

change groupbox header text via code wpf

I would like to change the label text of a group box outside the designer. I tried the content property but it it does not works. Any help ?
Create a label inside the design like this :
<GroupBox Height="608" Margin="0,0,0,0" Width="375" x:Name="grp1">
<GroupBox.Header>
<Label Foreground="Blue" x:Name="lblGroupHeader"></Label>
</GroupBox.Header>
</GroupBox>
then In code :
lblGroupHeader.Content = "The Name"

Character encoding for use within ItemTemplate

I'm creating a menu from a ListBox. I'm using FontAwesome to create some font-icons. This is part of the ListBox ItemTemplate.
<TextBlock FontFamily="FontAwesome" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="32" Text="{Binding MenuCode}"
ToolTip="{Binding Tooltip}" >
The problem resides within the TextBlock's Text. I need to display the symbol, not the menu code. So, for example, if I use Text="" directly, then the music icon appears (fixed for all the items), but when I use DataBinding (each item has a different symbol): Text="{Binding MenuCode}" then the text  (that is, the menu code as string) appears (as text, no icon). I guess problem is related with encoding, but can't fix it. Any idea?
Wrong escape sequence. HTML uses '#&x' while C# uses \u. So your "#&xF001" would become "\uF001"

How to display a Button which hosts TextBlock with the style PhoneTextSubtleStyle?

I need to create a button with two lines of text:
The first one is Command Title like "Save"
The second one is a Description of the Command like "The application state will be saved"
So I have written the next xaml:
<Button Margin="0,128,0,0" Padding="10,5" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<StackPanel Margin="0" UseLayoutRounding="False">
<TextBlock FontSize="{StaticResource PhoneFontSizeMediumLarge}" FontFamily="{StaticResource PhoneFontFamilySemiBold}">Save</TextBlock>
<TextBlock Style="{StaticResource PhoneTextSubtleStyle}" Margin="0">The application state will be saved</TextBlock>
</StackPanel>
</Button>
This code working well except a one issue. The Description line becomes invisible when the button is pushed.
I'm sure the root cause is the low contrast color of the description line. But I don't know how to fix it.
Update: I have tried to use the PhoneTextSubtleStyle style but still have the same issue.
You could retemplate the Button (using the Control.Template property) to look different so that when pushed it no longer interferes with the content.
Could you try something like this
System.Windows.Visibility.Visible;
System.Windows.Visibility.Hidden;
or
System.Windows.Visibility.Collapsed
here is a link that will show an example of how to use this inside of a StackPanel
How to: Change the Visibility Property

Categories

Resources