如何在WPF中使用绑定重新启用列表视图中的复选框?

时间:2013-03-18 18:31:16

标签: wpf

我的IsEnabled和IsChecked属性有一个多触发器。它运行良好,但禁用复选框后,它不会重新启用代码。这是XAML:

<ListView ScrollViewer.ScrollChanged="lstRewardsEarned_ScrollChanged" Height="178" HorizontalAlignment="Left" Margin="12,0,0,81" Name="lstRewardsEarned" VerticalAlignment="Bottom" Width="327" SelectionMode="Multiple" Background="White" FontFamily="Calibri" FontSize="16">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">

            <!-- bind content IsSelected to the ListViewItem IsChecked -->
            <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

            <!-- bind content IsEnabled to the ListViewItem IsEnabled -->
            <Setter Property="IsEnabled" Value="{Binding Path=IsEnabled, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="Border" SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>

                        <ControlTemplate.Triggers>

                            <!--Highlight if IsSelected and IsEnabled-->
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsEnabled" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter TargetName="Border" Property="Background" Value="Cyan"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <!--Remove highlight if is not Selected and IsEnabled-->
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="False"/>
                                    <Condition Property="IsEnabled" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter TargetName="Border" Property="Background" Value="Transparent"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>

                            <!--Add a trigger to remove the highlighting if IsEnabled is changed to false-->
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="Transparent"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

这是重新启用复选框的代码:

For Each checkBox As CheckBox In lstRewardsEarned.Items
    checkBox.IsEnabled = true

    'This writes out false!
    Console.WriteLine(checkBox.IsEnabled)
Next

提前致谢。

1 个答案:

答案 0 :(得分:0)

从IsEnabled和IsSelected属性绑定中取出UpdateSourceTrigger?

According to MSDN

  

如果将UpdateSourceTrigger值设置为Explicit,则必须调用   UpdateSource方法或更改不会传播回   源。