in Dev

Toggle WPF control visibility with DataTriggers

A frequent source of error while working with triggers is that I set the default value directly on the control itself (e.g. Label). Now when trying to change the value with a trigger that unfortunately won’t work. The default value has to be set in the control’s style as shown in the example below.

<Window x:Class="FlennyNET.Examples.DataTrigger.MainWindow"
        Title="DataTrigger Example" Height="350" Width="525">
        <StackPanel Orientation="Horizontal">
            <Label Content="DataTrigger Example">
                <Label.Style >
                    <Style TargetType="Label">
                        <Setter Property="Visibility" Value="Hidden" />
                            <DataTrigger Binding="{Binding HasValue}" Value="true">
                                <Setter Property="Visibility" Value="Visible" />