WPF: is there a way to do inline formatting? - c#

Is there a way to use inline formatting (like Swing's HTML tagging within labels) with WPF? I could not find any documentation.

The TextBlock (there may be others) can make use of several inline formatting elements.
[example lifted verbatim from: http://wpf.2000things.com/2011/03/14/245-easily-inline-text-formatting-codes-with-textblock-control ]
<TextBlock Margin="10" Height="100" FontSize="14" Width="290" TextWrapping="Wrap">
We <Bold>few</Bold>, we happy <Bold>few</Bold>, we band of <Underline>brothers</Underline>;
For he to-day that sheds his <Italic>blood</Italic> with me
Shall be my <Underline>brother</Underline>; be he ne'er so <Italic>vile</Italic>,
This day shall <Bold>gentle</Bold> his <Italic>condition</Italic>;"
</TextBlock>

Not quite sure if this is what you're looking for, but XAML uses attributes. Just like in HTML you can say <element attribute="foo"></element>

Related

Bold Matched search string in a AutoSuggestBox?

I'm a little surprised to see that this isn't a feature of the AutoSuggestBox but, I'm trying to configure an AutoSuggestBox to bold any matched text from what gets pulled up.
So it would look like:
[ jay ]
jaydeflix
jaydeflixutil
tommyjay
If anyone has sample code, I'll gladly look at it, but I'll even gladly take a pointer at what to dig into (I'm self-taught, so I'm used to digging, just my binggle-fu is coming up short on this one).
The suggestion list is actually a ListView of AutoSuggestBox. More details please see AutoSuggestBox styles and templates. To define a custom look for each item in the list, use the ItemTemplate property of AutoSuggestBox, for more details about this please check the Text changed section.
The TextBlock can be set with paticial bold text with <Run> tag that you can try to use inside the template. For example:
<AutoSuggestBox
x:Name="asb"
... >
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run FontWeight="Bold" Text="{Binding QueryString}"></Run>
<Run Text="{Binding DisplaySpare}"></Run>
</TextBlock>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
For more about how to contact the string to display, set the display order and bind to ItemsSource please try it yourself. This depends on the original source and your special requirements.

WPF - StringFormat with weighted text

I have a Question concerning WPF stringformat.
I want to bind a string to a TextBlock with a stringformat, currently I have something similar to the following:
<TextBlock>
<TextBlock.Text>
<Binding StringFormat="{}Text: {0}" Path="text"/>
</TextBlock.Text>
</TextBlock>
What I want to do is format the string to look as follows:
Text: text
Or in other words, I wish to Bold the first portion of the resulting string. In my research for a resolution to the problem I found a similar question, StringFormat with Font Weight. However, I'm not happy with that approach, and most suggestions echo this solution to my problem.
I would like to know if this is the only way to do this. I'm not quite happy with that solution as I feel it bloats the XML and makes it harder to read.
granted I am new to WPF overall so if this is the acceptable way I'll do so.... I just want to know if this is the best way to aproach this simple problem.
I only have one word for you: Run.
<TextBlock>
<Run FontWeight="Bold"
Text="Text: " />
<Run Text="{Binding text}" />
</TextBlock>
I should read the question entirely before answering; then again, you should not just link another page without adding details from it!
From the top of my head, there are only 2 solutions to have some markup formatting, <Run> and <Span>.
<Span> has some useful shorthands for <Bold>, <Italic> and <Underline>, but these do not support binding.
<Run> is verbose heavy, but supports binding.
In your case, the 'best' solution would be something like:
<TextBlock>
<Bold>Text: </Bold><Run Text="{Binding text}"/>
</TextBlock>
For the side note, Span is like Run, but can contains other elements (Run, Span, text).
For more details, see also.

Inline c# in XAML front-end: Why doesn't this work in XAML?

Scenario: Developing a "thick" client in UWP, using c# destined for the desktop of Windows 8.1/10 (a store app).
In my user interface, I tried to use the following inline
<StackPanel Grid.Column="0">
<TextBlock x:Name="Qty_Size_Crust" Margin="25,10,20,0" Padding="0,0,0,0" TextAlignment="Left" TextWrapping="Wrap" MaxHeight="25" Foreground="#ff230909" FontFamily="Segoe UI" FontSize="16" xml:space="preserve" HorizontalAlignment="Left">
if ({Binding WidgetName} == "Backend")
{
<Run Text="{Binding ItemCode, Converter=ItemCodeToDescriptionConverter}"/>
}
else
{
<Run Text="See Administrator}"/>
}
</TextBlock>
The compiler would accept this, but during execution, I would neither see Item Description nor See Administrator. Is there something else I should be doing in order for this work? I was able to work around this by putting the code in the ViewModel, but wondered why this didn't work (or maybe it shouldn't).
XAML doesn't work with inline scripts so the answer is simply - it's not supported.
In your example - the condition code is simply added as text runs in the TextBlock, so the compiler accepts that.
As for the best solution to your specific problem - a condition in the view model is a good one. A TemplateSelector would work on the view side, but requires more code. View model might actually also be the best place to put it if you want to be able to test it, so it really is the right way to do it. Putting logic in the view layer would simply add code that can't be unit-tested.

WP8 - C# - Getting object value from Telerik RadPickerBox

I am currently having an issue regarding this component.
This is the xaml usage of it in my app:
<telerikPrimitives:RadPickerBox x:Name="box_change" Width="0">
<StackPanel Background="White">
<StackPanel Height="618">
<PasswordBox Name="NewPassword1" Padding="2,8,2,8" Margin="10,0,10,0" Style="{StaticResource PasswordBoxStyle}" />
</StackPanel>
</StackPanel>
</telerikPrimitives:RadPickerBox>
I am currently in need of retrieving, from my C# code, the value "password" of the field "NewPassword1", still I am unable to do that.
I already tried retrieving it with something like this:
box_change.NewPassword1.Password
but, of course, else I wouldn't be asking this question right now, this way doesn't work.
What would you suggest to retrieve the value?
Thanks in advance,
Guido
this.NewPassword1.Password
Glad to help!
Have you considered using Telerik's RadPasswordBox for WP8? It's optimized for passwords with masking characters and other features (and easily provides you with the password):
var userEnteredPassword = myPasswordBox.Password;
That being said, even though your PasswordBox is the child of two StackPanels, you can access the Password box in the same scope via NewPassword1. If you have more complex controls see the control's documentation on how to get the value of a child element.

How to make <UIElement> interactable or click-able in WPF UI

This is my first day to design UI using WPF. I have looked up MSDN official document of Flow Document and found that I can place an UI control inside a RichTextBox. I did put a button in but found it's not interactable - I cannot click on it as it's grey. And I also tried other controls and they all displayed fine but just don't support interaction. Even a hyperlink doesn't work.
I have searched over internet, the closest question ever asked is about how to make an inside hyperlink click-able: The similar question: C# WPF Text with links
I did the same thing but it didn't work! All component displayed well but just are not able to be clicked.
Here is my XAML code:
<RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White" >
<FlowDocument>
<Section FontSize="15">
<Paragraph>
<Bold>Click on this:</Bold>
<Italic><Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink></Italic>
</Paragraph>
<BlockUIContainer>
<Button Click="Button_Click">Also Click On This</Button>
</BlockUIContainer>
</Section>
</FlowDocument>
</RichTextBox>
Can anyone give me some suggestion:
1. is it possible to make it click-able
2. if yes, if I forgot to set any/what attribution of the RichTextBox control?
First off your direct question: how to make the content of the RichTextBox "active". Set the IsDocumentEnabled property to True on the RichTextBox like shown here:
<RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White"
IsDocumentEnabled="True">
<FlowDocument>
<Section FontSize="15">
<Paragraph>
<Bold>Click on this:</Bold>
<Italic>
<Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
</Italic>
</Paragraph>
<BlockUIContainer>
<Button Click="Button_Click" >Also Click On This</Button>
</BlockUIContainer>
</Section>
</FlowDocument>
</RichTextBox>
Now to the unspoken question: do you have to be in a RichTextBox at all? The fact that there is a special property on the RichTextBox to make embedded UI elements active kinda indicates that is not the normal usage for this control. It is meant to host editable FlowDocument content. So the user of the RichTextBox would typically be creating the document that hosts the button that a consumer of the document could click, if that helps make the distinction clear I don't know.
However, all that being said, your FlowDocument hosted instead in a simple FlowDocumentPageViewer is active by default.
<FlowDocumentPageViewer>
<FlowDocument>
<Section FontSize="15">
<Paragraph>
<Bold>Click on this:</Bold>
<Italic>
<Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
</Italic>
</Paragraph>
<BlockUIContainer>
<Button Click="Button_Click" >Also Click On This</Button>
</BlockUIContainer>
</Section>
</FlowDocument>
</FlowDocumentPageViewer>
Now to the other unspoken question (unspeakable?) do you have to be in FlowDocument content at all? FlowDocument content is similar to, but not derived from UIElement. As such, many of the out-of-the-box features of UIElements are not available. If you need document functionality in the UI FlowDocuments can provide a great start but bring with them a pretty big learning curve in their own right.
The title of your question, if taken literally, makes me think you might just want a WPF UI that allows you to embed Buttons and Hyperlinks and have them work (gasp). That is certainly the default behavior. If you do not need the document look and feel that FlowDocument provides nor the real time document editing that RichTextBox provides you might consider a more "traditional" WPF layout.
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"
Margin="20">
<TextBlock>
<Bold>Click on this:</Bold>
<Italic>
<Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
</Italic>
</TextBlock>
<Button Click="Button_Click"
Margin="0,20,0,0">Also Click On This</Button>
</StackPanel>

Categories