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}."
Related
How can I display a x:Static variable in a MultiBinding Label?
<Label>
<Label.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="x:Static resources:AppResources.FirstName"/>
<Binding Path="User.FirstName"/>
</MultiBinding>
</Label.Text>
</Label>
Desired result:
First Name: John
Instead of Path, use Source.
<Label>
<Label.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Source="{x:Static resources:AppResources.FirstName}"/>
<Binding Path="User.FirstName"/>
</MultiBinding>
</Label.Text>
</Label>
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
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>
I have a Textbox with a Value and a Unit bound like this:
<TextBox Text="{Binding Path=Value,StringFormat='{}{0} mm'}" />
The Unit mm should be also bound to the ViewModel Property Unit. This can be done via a Multibinding:
<TextBox>
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Value"
Mode="TwoWay" />
<Binding Path="Unit"
Mode="OneWay" />
</MultiBinding>
</TextBox.Text>
</TextBox>
But with this I lose my Two Way Binding and I don't want to edit the Unit aswell. If ths user deletes "8 mm" and enteres an "8" the binding should automatically reevaluate the binding and add the unit as it is done via normal string format binding.
So finally I need something like this:
<TextBox>
<TextBox.Text>
<Binding Path="Value"
StringFormat="{Binding Path=ValueUnitStringFormat}" />
</TextBox.Text>
</TextBox>
But unfortunally StringFormat Property on BindingBase is not a DependencyProperty.
Anyone got a solution for this?
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;