I usually bind the textblock with string format and converter culture like this:
<TextBlock Text="{Binding CurrentPurchase.SubTotal, StringFormat='{}{0:C}', ConverterCulture='nl-NL'}"/>
But I have a multi binding with string format also that (price x quantity)
How can i put the ConverterCulture='nl-NL' here?
<TextBlock.Text >
<MultiBinding StringFormat='{}{0} x {1}'>
<Binding Path="Price"/>
<Binding Path="Quantity" />
</MultiBinding>
</TextBlock.Text>
Thank you
You can set it on the MultiBinding:
<TextBlock.Text >
<MultiBinding StringFormat='{}{0} x {1}' ConverterCulture="nl-NL">
<Binding Path="Price"/>
<Binding Path="Quantity" />
</MultiBinding>
</TextBlock.Text>
unless you need different cultures for Price and Quantity!
Related
I have a TextBlock with two properties (Text and Foreground) bound to the same ViewModel property.
Both also have converters. One of the converters checks the Text property and returns a 'dash' if the value is NaN. The other checks that the value is above, below or equals zero and accordingly sets the foreground to different colors.
XAML example:
<TextBlock>
<TextBlock.Text>
<Binding Path="AvgDistance" StringFormat="{}{0:N1}"
Converter="{x:Static converter:ValueToDash.Instance}"/>
</TextBlock.Text>
<TextBlock.Foreground>
<MultiBinding Converter="{x:Static converter:ValueToColor.Instance}">
<Binding Path="AvgDistance"/>
<Binding ElementName="currentPeriod" Path="IsChecked" />
</MultiBinding>
</TextBlock.Foreground>
</TextBlock>
Now I need that the ValueToDash converter fired before the ValueToColor converter, but it is always vice versa.
The Foreground property seems to be always set first, and only then the Text property is set.
Why is it so? And is it possible to reverse the order of setting?
You shouldn't rely on the order in which the properties are being set.
What you could do instead is to add another binding to your MultiBinding that binds to the Text property of the TextBlock:
<TextBlock>
<TextBlock.Text>
<Binding Path="AvgDistance" StringFormat="{}{0:N1}"
Converter="{x:Static converter:ValueToDash.Instance}"/>
</TextBlock.Text>
<TextBlock.Foreground>
<MultiBinding Converter="{x:Static converter:ValueToColor.Instance}">
<Binding Path="AvgDistance"/>
<Binding ElementName="currentPeriod" Path="IsChecked" />
<Binding Path="Text" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</TextBlock.Foreground>
</TextBlock>
Then the ValueToColor converter will be invoked (again) whenever the Text property is set to some new value.
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?
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}."