How to Convert this Statement into XAML? - c#

var FullName = String.Format("{0} {1}", "FirstName", "LastName");
How to translate to a TextBlock text?
<TextBlock Text="" />

StringFormat to the rescue:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
If you are wondering what the first {} does, it is to prevent WPF from thinking the first {0} is a MarkupExtension.

Whatever the identifier is for the TextBlock element, you need to use that in your code and do something along the lines of:
TextBlockVariable.Text = FullName;

Related

MultiBinding with StringFormat and localization using VisualBaml

I am writing a WPF application, which is then translated using VisualLocBaml. VisualLocBaml works mostly fine and allow me to translate all relevant strings, except for the StringFormat I use for a multibinding.
Here is the XAML portion of the code that has the multibinding:
<TextBlock x:Uid="TextBlock_3" TextAlignment="Center" FontSize="24">
<TextBlock.Text>
<MultiBinding x:Uid="MultiBinding_1" StringFormat="Your ladder position at {0} is {1}">
<Binding x:Uid="Binding_1" Path="localInfos.Town" />
<Binding x:Uid="Binding_2" Path="userInfos.Pos" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
I tried adding some Localization attribute as described here : https://msdn.microsoft.com/en-us/library/ms753944(v=vs.110).aspx, like this:
<TextBlock x:Uid="TextBlock_3" TextAlignment="Center" FontSize="24" Localization.Attributes = "$Text(Modifiable Readable)">
<TextBlock.Text>
<MultiBinding x:Uid="MultiBinding_1" StringFormat="Your ladder position at {0} is {1}" Localization.Attributes = "$StringFormat(Modifiable Readable)">
<Binding x:Uid="Binding_1" Path="localInfos.Town" />
<Binding x:Uid="Binding_2" Path="userInfos.Pos" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
But it didn't help: the StringFormat "Your ladder position at {0} is {1}" is still the only relevant string that is not available to translate in Visual LocBaml

Changing format xxxxxxxxxx to xxx-xxx-xxxx

I have a textbox in WPF and I need to display text in xxx-xxx-xxxx format.
<TextBox FontSize="30" Text="{Binding MyString,UpdateSourceTrigger=PropertyChanged}" Grid.Row="3" MaxLength="10"></TextBox>
MyString is just property which sets value in TextBox into it for some other logic. Can I do it in XAML itself usng StringFormat?
You can try using the MaskedTextBox to specify the format of the input.
Example:
<wpfx:MaskedTextBox Mask="000-000-0000" />
Try this tutorial also.
As an option you could split the text in the code behind (or View Model) and bind each value separately
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{0}-{1}-{2}">
<Binding Path="FirstPart" />
<Binding Path="SecondPart" />
<Binding Path="ThirdPart" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Yes you can do this. Try the following:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}-whateverhercomes-andwhateverherecomes">
<Binding Path="MyString"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>

Using localized formatted string in WPF binding

I have a formatted string in a .resx file like so:
Blah: {0}
How do I use it in WPF binding to fill the {0} part?
i didn't tested it but...
https://social.msdn.microsoft.com/Forums/vstudio/en-US/f77ab886-2def-4cef-aed3-9ced24eb5776/using-stringformat-in-a-textblock-in-wpf?forum=wpf
so i guess you should do something like this :
<TextBlock Text="{Binding Path=MyStringParameter, StringFormat={Binding MyStringFormatResource}"/>
In addition Binding/StringFormat you can also use MultiBinding element.
<StackPanel>
<TextBox Name="countText" Text="4" />
<TextBox Name="totalText" Text="10" />
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Select {0} of {1}">
<Binding ElementName="countText" Path="Text" />
<Binding ElementName="totalText" Path="Text" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
In a real sample you would bind something else but XAML elements but this shows you the idea.

XAML MultiBinding StringFormat

I cannot figure out my MultiBinding StringFormat behavior. I have a DataContext which defines a numeric range via the properties MinIncl and MaxIncl. I want to create a tooltip using MultiBinding to create a tip like "1.0 to 999.0". I try the following code:
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:F1} to {1:F1}">
<Binding Path="SelectedTrainingScriptParameter.MinimumInclusive"/>
<Binding Path="SelectedTrainingScriptParameter.MaximumInclusive"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</ToolTipService.ToolTip>
The resulting tooltip is "to 999.0 1.0". So it has reversed the range and put the word "to" first.
If I remove the spaces and try "{}{0:F1}to{1:F1}", I get the right answer: "1.0to999.0".
Seriously, why does the whitespace and word "to" break this thing?
Thanks.
-reilly.
In this case you don't need a multi, just use the same TextBlock instead like;
<TextBlock>
<Run Text="{Binding Path=SelectedTrainingScriptParameter.MinimumInclusive}"/>
<Run Text="to"/>
<Run Text="{Binding Path=SelectedTrainingScriptParameter.MaximumInclusive}"/>
</TextBlock>
Or if you really want to use it as is;
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} to {1}">
<Binding Path="SelectedTrainingScriptParameter.MinimumInclusive" />
<Binding Path="SelectedTrainingScriptParameter.MaximumInclusive" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
As to the weird whitespace thing, no idea sorry. Hope this helps, cheers.

StringFormat and Multibinding with Label

I would like to use StringFormat to do someting like this :
<Label x:Name="myLabel">
<Label.Content>
<Multibinding StringFormat="{}{0} - {1}">
<Binding Path="Lib1" />
<Binding Path="Lib2" />
</MultiBinding>
</Label.Content>
</Label>
However, it's doesn't work and I got this error instead :
MultiBinding failed because it has no valid Converter. MultiBindingExpression:target element is 'Label' (Name='myLabel'); target property is 'Content' (type 'Object')
Is there any way to make this code work ?
You cant bind this because you are trying to bind a string to an object which wont work because StringFormat requires its target to be a string type. You can get around this by either using a TextBlock instead (which has a Text property) or putting the Textblock as the child of the Label:
<Label x:Name="myLabel">
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Lib1" />
<Binding Path="Lib2" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Label.Content>
</Label>
For those wondering you can also leave the <Label.Content> tag from Leom Burke's answer. This saves another two lines of code.
<Label x:Name="myLabel">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Lib1" />
<Binding Path="Lib2" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Label>
<Label>
<AccessText>
<MultiBinding StringFormat="{x:Static properties:Resources.MyText}">
<Binding Path="MyObj.MyProp" Mode="OneTime"/>
</MultiBinding>
</AccessText>
</Label>
Where Resources.MyText can hold anything like "Fox jumps over {0}."

Categories

Resources