String written in different fonts - c#

In my UWP app I'm trying to add a Button which has written on it an icon of the Segoe MDL2 font family and today's date, the problem is that I cannot show both the icon and the text due to the fact that they belong to different font families, is there a way to show both?
The button is defined like this in the XAML code:
<Button x:Name="button" Content=" " FontFamily="Segoe MDL2 Assets" AutomationProperties.Name="Favorite" />
And the code I use to update the content property of the button is this:
button.Content = "" + date;

With WPF the trick is to use a TextBlock as your button's content. I am assuming this also works in UWP, but I am not 100% sure.
<Button>
<Button.Content>
<TextBlock>
<Run FontFamily="Arial">Hello </Run>
<Run FontFamily="Courier New">Mr. Bob</Run>
<Run FontFamily="Arial">, you have foo.</Run>
</TextBlock>
</Button.Content>
</Button>
With the TextBlock you can add multiple Run objects to explicitly define different formats for each section of text.

Related

WPF textBox changes color halfway

I have a strange problem with the textBox in WPF. I have a simple textbox inside a stackpanel binded with some text on the code behind. When the text becomes too long some part of it changes color. Here is an example
The stackpanel is this
<StackPanel Canvas.Left="50" Canvas.Top="111">
<TextBlock Text="{Binding Title}" FontSize="32" Foreground="White" />
<TextBlock Text="{Binding Subtitle}" FontSize="15" Foreground="#9EA3AA"/>
</StackPanel>
How can I make all the text white?
As mentioned by Clemens you are experiencing overlap. To fix this make the cell containing the orange cpu image and text not extend so high. You should be able to just drag and drop.

Add multiline content to button in c# windows phone 8.1

I have an app with some button which contains different text, of different length. When the text is longer than the width of the button, only the first part of the text is displayed.
Is there a way to dynamically split the text in 2 lines or more?
Thanks
Use TextBlock to define button content and set TextWrapping property.
<Button>
<TextBlock TextWrapping="Wrap">your text</TextBlock>
</Button>
For specific breaks in your text I like to use Runs and LineBreaks
<Button>
<TextBlock TextWrapping="Wrap" FontSize="9">
<Run Text="Bind some text here" />
<LineBreak/>
<Run Text="Add some more text" />
</TextBlock>
</Button>

Accessing and using a datatemplate textbox in xaml.cs

Normally when I create a textbox I can use it in visual studios like so.
xaml
<TextBlock Name="BedNumberTextBlock" FontSize="26" Foreground="White" FontFamily="Segoe UI" HorizontalAlignment="Left" Margin="10,0">
c#
BedNumberTextBlock.Visibility =Visibility.Collapsed;
However when my textblock is enclosed in a data template I cannot find it to control its visibility. Could some one show me how to find the textblock and collapse its visibility please?

How to textwrap contents of multiple controls aligned horizontally so that it looks like a single string?

I have three controls aligned horizontally, a ListPicker, a Textblock and a ListPicker.
The content would look like,
United States of America v/s United Kingdom
United States of America - content of first ListPicker (changes the value based on item selected)
v/s - Textblock content
United Kingdom - content of second ListPicker (changes the value based on item selected)
I need to textwrap this whole string "United States of America v/s United Kingdom" as if it is the content of a single control.
Is this possible? Please help.
You could try using a RichTextBox with InlineUIContainers:
<RichTextBox>
<Paragraph>
<InlineUIContainer>
<ListBox>
<ListBoxItem>United states of America</ListBoxItem>
<ListBoxItem>United states of America</ListBoxItem>
</ListBox>
</InlineUIContainer>
<Run Text="vs" />
<InlineUIContainer>
<ListBox>
<ListBoxItem>United Kingdom</ListBoxItem>
<ListBoxItem>United Kingdom</ListBoxItem>
</ListBox>
</InlineUIContainer>
</Paragraph>
</RichTextBox>
But this still won't wrap the text inside the InlineUIContainer. It will wrap only the parts relative to one another.
If you want to be able to wrap the inside text, you need to change your approach. The only solution I can think of is to use Hyperlinks:
<RichTextBox>
<Paragraph>
<Hyperlink Click="Hyperlink1_Click">United states of America</Hyperlink>
<Run Text=" vs " />
<Hyperlink Click="Hyperlink2_Click">United states of America</Hyperlink>
</Paragraph>
</RichTextBox>
and to handle the Hyperlink.Click event and instead of navigating, to display a list of items to be picked and then change the text of the hyperlink to reflect the new selected item. You may also want to try to restyle the hyperlinks
you can use stackpanel as follow's:
<StackPanel Orientation="Horizontal" >
<toolkit:ListPicker VerticalAlignment="Center" Width="100"/>
<TextBlock Text="vs" VerticalAlignment="Center" TextAlignment="Center"/>
<toolkit:ListPicker VerticalAlignment="Center" Width="100" />
</StackPanel>
Note: "Width" depends on you.

How to handle subtext Tap?

In Windows Phone (on both Windows Phone 7.1 and Windows Phone 8), in a block of text with wrapping I want a subtext to be underlined and clickable. However, Runs in TextBlock/RichTextBox don't have any events. Using WrapPanel of WPToolkit I could achieve the desired wrapping by putting each words in a separate TextBlock, but i loose the ability to set TextAlignment to Justify. Is there a better way to do this?
<TextBlock TextWrapping="Wrap">
<Run Text="I have a paragraph consisting of multiple lines that I want to be neatly wrapped with just one word that I'll be able to "/>
<Run TextDecorations="Underline">tap</Run>
</TextBlock>
<toolkit:WrapPanel>
<TextBlock Text="I "/>
<TextBlock Text="have "/>
<TextBlock Text="a paragraph "/>
<TextBlock Text="consisting "/>
...
<TextBlock Text="able to"/>
<TextBlock TextDecorations="Underline" Tap="HandleLinkTap" Text="tap"/>
</toolkit:WrapPanel>
Does https://stackoverflow.com/a/15148602/546896 solves your problem?
I know little hackish way but still see if it works.
<RichTextBox TextWrapping="Wrap">
<Paragraph>
<Run Text="I have a paragraph consisting of multiple lines that I want to be neatly wrapped with just one word that I'll be able to " />
<Hyperlink Click="HandleLinkTap">Tap</Hyperlink>
</Paragraph>
</RichTextBox>

Categories

Resources