Concatenate StaticResources in WPF - 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>

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.

wpf set property to static resource with string manipulation

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.

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>

Change Color of Single Letter in Label String?

I have a project in WPF 4 and VB.net. I need to change the color a single letter in a word in a label (the label's content changes quite a bit). I really am not sure if this is possible, but if it is, I'd appreciate help on figuring out how. TY!
Label is a content control so any type of content is permitted inside a label.You can easily do your requirement by something like
<Label>
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="Red" Text="T"/>
<TextBlock Text="ext"/>
</StackPanel>
</Label>
A cleaner way would be using the flow-content-capabilites of a TextBlock:
<Label>
<TextBlock>
<Run Text="L" Foreground="Green"/>
<Run Text="orem Ipsum"/>
</TextBlock>
</Label>
This limits binding a bit though, if that is needed.
The cleanest method i found so far is using a TextEffect:
<Label>
<TextBlock Text="Search">
<TextBlock.TextEffects>
<TextEffect PositionStart="0" PositionCount="1" Foreground="Red"/>
</TextBlock.TextEffects>
</TextBlock>
</Label>
This colors the "S" red. You can of course bind any of the involved properties if they need to be dynamic.
I just implemented something like this in our project, this will be static though - I'm not sure if that's what you need. You can change the content of the label as often as you need, but it will always have a red * at the end. I added a style to the project like this
<Style x:Key="RequiredFieldLabel"
TargetType="{x:Type Label}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
<TextBlock Text="*"
Foreground="red" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Then you can use this style on a label anywhere in your project.
<Label Content="Enter Name:"
Style="{StaticResource RequiredFieldLabel}" />

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