简体   繁体   中英

XAML DataTrigger Not Firing?

I have a property in my ModularClientModule class called ExistsOnDisk . When that property is set to false, I want to disable the CheckBox. For some reason, this does not seem to work despite me putting it last in the Triggers XAML block, which means it should be the last thing to fire. Ie, nothing should be interfering. I know my code-behind should be correct because as you will see below, I have another control which receives the update and fires fine.

This one here responds to the ExistsOnDisk property change without issue, and strikesthrough the text:

GridViewColumn x:Name="FileNameHeader" Header="File Name">
<GridViewColumn.CellTemplate>
    <DataTemplate DataType="{x:Type local:ModularClientModuleUI}">
        <TextBlock Text="{Binding Filename}">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ExistsOnDisk}" Value="False">
                            <Setter Property="TextDecorations" Value="Strikethrough"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

This one here does not, and the checkbox remains enabled:

<GridViewColumn x:Name="StatusHeader" Header="Status">
    <GridViewColumn.CellTemplate>
        <DataTemplate DataType="{x:Type local:ModularClientModuleUI}">
            <StackPanel>
                <CheckBox x:Name="ClientModuleStatusCheckBox" Margin="5, 0" Click="ClientModuleStatusCheckBox_Click" IsEnabled ="{Binding Client.Online}">
                    <CheckBox.Style>
                        <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource MahApps.Styles.CheckBox}">

                            <Setter Property="IsEnabled" Value="False" />

                            <Style.Triggers>
                                <DataTrigger Binding="{Binding LoadState}" Value="Loaded">
                                    <Setter Property="IsChecked" Value="True" />
                                    <Setter Property="IsEnabled" Value="True" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding LoadState}" Value="Unloaded">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding LoadState}" Value="Loading">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>

                                <DataTrigger Binding="{Binding ExistsOnDisk}" Value="True">
                                    <Setter Property="IsEnabled" Value="True" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ExistsOnDisk}" Value="False">
                                    <Setter Property="IsEnabled" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </CheckBox.Style>
                </CheckBox>

What could be going on here?

Set the default value of the IsEnabled using a Setter and remove IsEnabled ="{Binding Client.Online}" from the <CheckBox> element:

<CheckBox x:Name="ClientModuleStatusCheckBox" Margin="5, 0" Click="ClientModuleStatusCheckBox_Click">
    <CheckBox.Style>
        <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource MahApps.Styles.CheckBox}">

            <Setter Property="IsEnabled" Value="False" />

            <Style.Triggers>
                <DataTrigger Binding="{Binding LoadState}" Value="Loaded">
                    <Setter Property="IsChecked" Value="True" />
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
                <DataTrigger Binding="{Binding LoadState}" Value="Unloaded">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding LoadState}" Value="Loading">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>

                <DataTrigger Binding="{Binding ExistsOnDisk}" Value="True">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>

            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

Also note that you shouldn't use more than one trigger on the same bool property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM