How to make a trigger aware of a property change

RogerSchlueter-7899 1,446 Reputation points
2021-05-22T23:34:55.51+00:00

On a wpf window I have a button defined as follows:

<Button
    x:Name="btnDone"
    Content="Done">
    <Button.Style>
        <Style
            TargetType="Button">
            <Setter Property="Visibility" Value="Collapsed" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsDrawing}" Value="True">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

The trigger property, IsDrawing, is defined as folows:

 Public IsDrawing As Boolean

When the user initiates drawing I set IsDrawing = True but the trigger does not make the button visible.

I have tried to make IsDrawing a dependency property of the window, adding a PropertyChanged event to the window and other - equally unsuccessful - ideas. None, however, make the trigger aware of the change.

How do I accomplish that?

Developer technologies Windows Presentation Foundation
Developer technologies VB
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-05-23T07:59:44.24+00:00

    Probably you made some mistakes or forgot to set the DataContext (which can be done in constructor or Load event, for example). Show some details.

    The next fragments of MainWindow window seems to work.

    XAML:

    <Window x:Class="MainWindow"
            . . .
            Loaded="Window_Loaded">
        <Grid>
            <Button x:Name="btnDone" Content="Done" Margin="24,68,612,297">
                <Button.Style>
                    <Style
                 TargetType="Button">
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=IsDrawing}" Value="True">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
            <Button x:Name="testButton" Content="test" HorizontalAlignment="Left" Margin="355,31,0,0" VerticalAlignment="Top" Width="75" Click="testButton_Click"/>
        </Grid>
    </Window>
    

    Class:

    Class MainWindow
    
        Private Shared ReadOnly Property IsDrawingProperty As DependencyProperty = DependencyProperty.Register(NameOf(IsDrawing), GetType(Boolean), GetType(MainWindow))
    
        Public Property IsDrawing As Boolean
            Get
                Return CBool(GetValue(IsDrawingProperty))
            End Get
            Set(value As Boolean)
                SetValue(IsDrawingProperty, value)
            End Set
        End Property
    
        Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
            DataContext = Me
        End Sub
    
        Private Sub testButton_Click(sender As Object, e As RoutedEventArgs)
            IsDrawing = True
        End Sub
    
    End Class
    

    This example assumes that IsDrawing is a member of your window.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.