How to write conditional statements in WPF?

Possible Duplicate:
XAML Conditional Compilation

I am new to WPF. I just need to write a small piece of code in xaml, for which I need to know the if equivalent in WPF. Can anyone here help with this?

+5
source share
1 answer

You after something like: "If (x == 1), make the background of this control blue"? If this is what you need, you can use data triggers. Here is an example that changes the background color of a control based on certain data. In this example, I made it part of the style and used it later in some controls.

<UserControl.Resources>
    <Style x:Key="ColoringStyle" TargetType="{x:Type DockPanel}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Coloring}" Value="Red">
                <Setter Property="Background" Value="#33FF0000"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Coloring}" Value="Blue">
                <Setter Property="Background" Value="#330000FF"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Coloring}" Value="White">
                <Setter Property="Background" Value="#33FFFFFF"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

"" "", "" "", DockPanel.

<DockPanel Style="{StaticResource ColoringStyle}">
     ...
</DockPanel>
+16

All Articles