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.
Related
I'm currently working on a .NET Framework 4.7.1 WPF application. I need to localize a string in a TextBlock element, using standard .resx files.
The problem is, in my TextBlock, I use a dynamic resource, consisting out of a text and an increasing number (counter).
<TextBlock Text="{Binding LoadingPercent, StringFormat=Loading the app...{0:N0}%}" />
Do you know how to localize this text "Loading the app..." in XAML?
Thank you very much!
Localize only "Loading the app..." and split the TextBlock into two Run elements:
<TextBlock>
<Run Text="{x:Static local:Resources.LoadingLabel}" />
<Run Text="{Binding LoadingPercent, StringFormat=P0}" />
</TextBlock>
You will have to move the format string part to a resource, and use this with MultiBinding like the following:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{x:Static local:Resource1.LoadTheAppFormated}">
<Binding Path="LoadingPercent"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
EDIT:
Your resource entry Resource1.LoadTheAppFormated should of course contain the whole formatted string "Loading the app...{0:N0}%".
For Localizing you will then need the extra *.en.resx (How to use localization in C#)
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.
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>
I have some textblocks composed of Runs. I want the characters within the textblock to be individually selectable like one would expect.
Example XAML:
<TextBlock>
<Run Text="Cant-individually-select" />
<Run Text="Still-Cant-individually-select" />
</TextBlock>
Is there any way to make the run inside the textblock behave like a standalone run? I see there a lot of solutions involving styling a TextBox to look like a TextBlock, but this isn't possible when I need multiple runs.
If I want to bind something like a combobox in the code-behind I have no problem at all. Something like :
List<string> strings = new List<string>();
AddStringsFromDataSourceToList(strings);
comboBox1.ItemSource = strings;
As far as I can tell, there is no quick and dirty way to do this in XAML. For all of the praise wpf is receiving for its super simple databinding, something this simple seems far easier to just do in C#. Is there an easier way to do this than creating DependencyProperty wrappers and adding them as resources without much help from intellisense or all that goes into ObservableCollections? I understand that its not impossible, but I must be missing something if such a simple task seems so in depth...
EDIT: To clarify, adding dynamic Lists is the issue here, not static arrays. It is super easy to add items manually, as many have pointed out.
<Window.Resources>
<x:Array x:Key="strings" Type="sys:String"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
</x:Array>
<!-- likewise -->
<x:Array x:Key="persons" Type="{x:Type local:Person}"
xmlns:local="clr-namespace:namespace-where-person-is-defined">
<local:Person FirstName="Sarfaraz" LastName="Nawaz"/>
<local:Person FirstName="Prof" LastName="Plum"/>
</x:Array>
<Window.Resources>
<ComboBox ItemsSource="{StaticResource strings}" />
<ListBox ItemsSource="{StaticResource persons}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding FirstName}"/>
<Run Text=" "/>
<Run Text="{Binding LastName}"/>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Are you looking for something like this:
<ComboBox>
<ComboBox.ItemsSource>
<x:Array Type="sys:String"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>
If you only want to define some items for a list, look at the solution from Henk Holterman. By the way, you can declare the array also as a resource and for other types.
Update
It seems that you have changed your question. However I don't understand now what your desire is. If you want to bind a collection you have in your code-behind, then make a public property that returns this collection, set the DataContext to the object that exposes this property and define a Binding in XAML:
<ComboBox ItemsSource="{Binding NameOfYourCollectionProperty}"...
Hope I understood your question right...
Sure there is:
<ComboBox>
<ComboBoxItem>One</ComboBoxItem>
<ComboBoxItem>Two</ComboBoxItem>
</ComboBox>
There are other syntaxes depending on your goal that are nearly as simple - using resources or inline itemssources, or grouped data.. even xml data. Don't throw up your hands in frustration because the first thing you tried wasn't easy - wpf is worth the learning curve, in my opinion.
WPF gets praise because it makes separating the visuals from the behavior much easier than windows forms, and because it makes creating nice visual effects much much easier, not because it makes it easier to do trivial examples. However, in this case - it is easier to do the trivial example.
With your edit Where do you want to pull them from? You don't have to create dependency properties or observable collections by any means. A simple list property will do (I prefer to use a collectionviewsource in the xaml in that case). Also, don't forget that you don't need to use all XAML if you hate it. But if you design for WPF instead of in spite of it you'll find a lot of tasks (like this one) easy.
You can do:
<ComboBox>
<ComboBoxItem>one</ComboBoxItem>
<ComboBoxItem>two</ComboBoxItem>
</ComboBox>
And there also exists syntax for declaring (and implicitly creating) data in for example a Resource section.
Maybe you can point out a more full scenario, with the requirements and constraints ?