Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xaml conditional StringFormat

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?

like image 830
Guillaume Paris Avatar asked Dec 05 '16 16:12

Guillaume Paris


1 Answers

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>
like image 141
EgoPingvina Avatar answered Sep 30 '22 06:09

EgoPingvina