Search code examples
wpfbindingvisibilitydatatrigger

Running the trigger only when the binding is equal to a certain value


I want to run a trigger that makes visibilty to Visible only when the binding is a certain value, in another case so visibilty is Hidden.

Here my grid:

    <Grid>
        <Grid.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MyProp}" Value="10">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
     </Grid>

How do I do that in any other case, the visibility will be Hidden?


Solution

  • Set the default value as hidden then in your style. By default value will always be hidden and will turn to visible only in case value is 10 -

     <Grid>
        <Grid.Style>
            <Style>
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MyProp}" Value="10">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
     </Grid>