I would like to make a binding
with a "conditional" price format.
More precisely, If another property is at true
: display price without percentage, if it is at false
, display price with percentage. Is there a way to handle this case with xaml or should I just make a string price property in my code-behind code?
You can use DataTrigger
like this WPF DataBinding with an conditional expression. And for other formats of value you can use different Converters
.
For example:
<UserControl.Resources>
<converters:ToPercentage x:Key="ToPercentage"/>
</UserControl.Resources>
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AnotherProperty}" Value="True">
<Setter Property="Text" Value="{Binding Path=Price}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=AnotherProperty}" Value="False">
<Setter Property="Text" Value="{Binding Path=Price, Converter={StaticResource ToPercantage}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With