wpf set property to static resource with string manipulation - wpf

I'm creating a WPF application. It has two labels which use the same static string resource, but with some differences. For example, it has a string resource with the key string1 and value SuccessRate. I want the first label to be SuccessRate and the second Label to be SuccessRate(%). I define the first label with:
<Label Content="{StaticResource string1}" />
How can I define the second Label?

You might set the Content of the second Label to a TextBlock with two Run elements:
<Label>
<TextBlock>
<Run Text="{StaticResource string1}"/>
<Run Text="(%)"/>
</TextBlock>
</Label>
Perhaps you need to have only TextBlocks instead of Labels anyway:
<TextBlock Text="{StaticResource string1}"/>
<TextBlock>
<Run Text="{StaticResource string1}"/>
<Run Text="(%)"/>
</TextBlock>

You can use ContentStringFormat
<Label Content="{StaticResource string1}" ContentStringFormat="{}{0}(%)" ... />
Note that format starts with {}. It's just something what has to be there if your format starts with. {
You can read about ContentStringFormat on MSDN.

Related

Concatenate hard coded value to ViewModel property via XAML?

I have a simple Label in WPF as under:-
<Label Content="{Binding MyViewModel.SomeValue,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
I tried this but it didn't work:
<Label Canvas.Top="26" Canvas.Left="253" Content="{Binding "Hardcoded String"+CurrentRec.Current_Vendor_Purchase_Record.TaxName,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
and this:
<Label Canvas.Top="26" Canvas.Left="253" Content="Hard Coded String Value"+ "{Binding CurrentRec.Current_Vendor_Purchase_Record.TaxName,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
How can i concatenate some hard coded value to MyViewModel.SomeValue from XAML side?I mean i can always do it from code-behind,but just wanted to know how to add a hard coded value to a ViewModel property in XAML DataBinding?
I know it shouldn't be difficuilt,but plz discount me as a beginner :-).
You could use two different Run elements:
<Label Canvas.Top="26" Canvas.Left="253">
<Label.Content>
<TextBlock>
<Run Text="Hardcoded String" /><Run Text="{Binding CurrentRec.Current_Vendor_Purchase_Record.TaxName, Mode=OneWay}" />
</TextBlock>
</Label.Content>
</Label>
You don't event need to use a Label element:
<TextBlock Canvas.Top="26" Canvas.Left="253">
<Run>Hardcoded String</Run><Run Text="{Binding CurrentRec.Current_Vendor_Purchase_Record.TaxName, Mode=OneWay}" />
</TextBlock>
This will display the concatenated values in the view.

Concatenate StaticResources in WPF

I need to concatenate two strings from string resources into a label.
I want something like this
<Label Content="{StaticResource Menu + " " + Name}"></Label>
How can I do that?
Any help is appreciated.
when Content is a string, framework creates a TextBlock behind the scenes to display it. So
<Label Content="Smth"/>
is transformed into
<Label>
<TextBlock Text="Smth"/>
</Label>
you can add TextBlock in xaml and explicitly assign two strings from Resources:
<Label>
<TextBlock>
<Run Text="{StaticResource Menu}"/>
<Run Text="{StaticResource Name}"/>
</TextBlock>
</Label>

How to dynamically display formatted text in TextBlock wpf

I have some text in a resx file that I need to display in a TextBlock. This text will have Bold areas that I will have to show in Bold. What is the best way to maintain Bold areas when the text changes? I cannot do inline from code behind because the text in resx file can change. WPF vb.net application. Thanks
You need to use Inlines:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="{Binding BoldTextProperty}" />
<Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicandRedColorTextProperty}" />
</TextBlock.Inlines>
If you are not using binding, assign the text directly to the Text property.
You can also bind the other properties:
<TextBlock.Inlines>
<Run FontWeight="{Binding Weight}"
FontSize="{Binding Size}"
Text="{Binding LineOne}" />
<Run FontStyle="{Binding Style}"
Foreground="Binding Colour}"
Text="{Binding LineTwo}" />
</TextBlock.Inlines>
You can bind through converters if you have bold as a boolean (say).
I had to make some changes. I removed the text from resx file and put it into a .txt file instead. I created a .txt file for each body under a header and hard coded the headers. Using Inline from code behind I controlled what goes in the header and what goes under it as the body. This way body of the text can be changed in the .txt files and code will stay the same.

Data Binding Data Formatting Issue

I have a AutoCompleteBox bound to the datasource. DataSource contains two string
properties. I have defined ItemTemplate for AutoCompleteBox.
I want second property to come closed in brackets e.g. Property1 Data (Property2 Data)
I will have to define StringFormat during Binding.
I am totally unaware of format. Can anyone tell me the format.
Thanks.
here is examples.
A simple way to use StringFormat in the binding.
Output: (0)
<TextBlock Text="{Binding Videos.Count, StringFormat='({0})', FallbackValue='(0)'}" />
Using <Run> tags you can also build complex values.
Output: Distance: 200km
<TextBlock>
<Run Text="Distance: " />
<Run Text="{Binding VideoDistance, StringFormat='\{0:G\}'}" />
</TextBlock>
If you use the second example, you must just add another <Run> tag for the next value.
You can also use a horizontal StackPanel to show multiple values.
Output: Start Distance: 200km
<!--START DISTANCE MIN-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Start Distance:" />
<TextBox Text="{Binding StartDistanceMinStr, Mode=OneWay}" IsReadOnly="True" />
</StackPanel>

WPF: Is there a sort of XAML tag that behaves like the HTML span element?

Coming from ASP.NET, this WPF stuff is just confusing. All I want to do is put a red asterisk by a label to indicate a required field. Playing around with stuff, I found that this actually does the trick:
<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
<Label Foreground="Red" Content="*" /><Label Content="Heavy Weight" />
</TextBlock>
Being that I just came up with this, I am not convinced it's the academic route a seasoned WPF developer would take. Additionally, this markup puts a huge amount of white space in between the asterisk and the label. In HTML, a span element would just render right beside its next sibling element. FYI, I tried putting a label within a label, but VS2010 kept barking about "The property 'content' is set more than once".
Any ideas?
Something like this would be more appropriate:
<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
<Span Foreground="Red">*</Span>Heavy Weight
</TextBlock>
Here is an overview of what can go into a TextBlock's content, more specifically here.
one more way is
<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
<Run Foreground="Red" Text="*" />
<Run Text="Heavy Weight" />
</TextBlock>
btw
Damascus's solution adds more UI Elements.
with CodeNaked's solution, its difficult to databind the Text.
The explanation is that you actually put two elements one after the other. You need to put them into a container.
Just a sample code of a sentence with red asterisk I did recently:
<StackPanel Orientation="Horizontal" Margin="5" >
<TextBlock Text="Display name"/>
<TextBlock Text="*" Foreground="Red" FontWeight="Bold" />
<TextBlock Text=":"/>
</StackPanel>
There, everything is in a StackPanel, so property 'content' will actually be set once (if you don't specify a group panel such as this one, you'll have to add only one element)

Resources