datatrigger无法设置绑定属性

时间:2014-04-08 09:04:20

标签: wpf xaml binding triggers

我有2个复选框(chkMfsUi和chkMfs)。当我检查chkMfsUi时,我也想检查chkMfs(并禁用它)。

我用数据触发器尝试了它:

        <Style x:Key="MfsCheckBoxStyle" TargetType="CheckBox">
            <Setter Property="IsEnabled" Value="True" />
            <Setter Property="IsChecked" Value="False" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=chkMfsUi}" Value="True">
                    <Setter Property="IsEnabled" Value="False" />
                    <Setter Property="IsChecked" Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

这些是我的复选框:

<StackPanel>
    <CheckBox Name="chkMfsUi"
                Checked="CheckBox_Checked"
                Content="MFS - UI"
                IsChecked="{Binding MfsUi}"
                Unchecked="CheckBox_Checked" />
    <CheckBox Content="MFS" IsChecked="{Binding Mfs}" Style="{StaticResource MfsCheckBoxStyle}" />
</StackPanel>

IsEnabled属性工作正常,但IsChecked没有。也许是因为它受约束?我也在使用带有这些属性的INotifyPropertyChanged。

如果我在代码后面设置它,它可以工作,但是可以使用触发器吗?

此致

阿尔方

修改

从目前的答案中可以看出,我的问题似乎不完整。我需要有以下三种状态:

Possible states of the checkboxes

最后但并非最不重要:两个复选框&#39;必须绑定IsChecked属性! (这是麻烦来临的时刻)

2 个答案:

答案 0 :(得分:1)

试试这个

  <StackPanel Grid.Column="1">
        <CheckBox x:Name="chkMfsUi" IsChecked="True"></CheckBox>
        <CheckBox x:Name="chkMfs" IsChecked="{Binding ElementName=chkMfsUi,Path=IsChecked}">
            <CheckBox.Style>
                <Style TargetType="CheckBox">
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="IsEnabled" Value="False"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </CheckBox.Style>
        </CheckBox>

另一种方法:使用资源中的样式

首先添加此命名空间xmlns:Globalvaribale =“clr-namespace:System; assembly = mscorlib”

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:Globalvaribale="clr-namespace:System;assembly=mscorlib">

<Window.Resources>
    <Globalvaribale:String x:Key="chkMfsUi">chkMfsUi</Globalvaribale:String>
    <Style x:Key="chkMfsstyle" TargetType="CheckBox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsChecked,ElementName={StaticResource chkMfsUi}}" Value="True">
                <Setter Property="IsEnabled" Value="False"/>
                <Setter Property="IsChecked" Value="True"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<StackPanel Grid.Column="1">
    <CheckBox x:Name="chkMfsUi"></CheckBox>
    <CheckBox Style="{StaticResource chkMfsstyle}"/>
</StackPanel>

<强>输出

enter image description here         

答案 1 :(得分:0)

在这种情况下,您可以使用EventTrigger

  

表示应用一组操作以响应事件的触发器。

示例:

<StackPanel>
    <StackPanel.Triggers>
        <EventTrigger RoutedEvent="CheckBox.Checked" SourceName="First">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetName="Second"
                                                    Storyboard.TargetProperty="IsChecked">

                        <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True" />
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <EventTrigger RoutedEvent="CheckBox.Checked" SourceName="Second">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetName="First"
                                                    Storyboard.TargetProperty="IsChecked">

                        <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True" />
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <EventTrigger RoutedEvent="CheckBox.Unchecked" SourceName="First">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetName="Second"
                                                    Storyboard.TargetProperty="IsChecked">

                        <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <EventTrigger RoutedEvent="CheckBox.Unchecked" SourceName="Second">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetName="First"
                                                    Storyboard.TargetProperty="IsChecked">

                        <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </StackPanel.Triggers>

    <CheckBox Name="First"
              Content="First" />

    <CheckBox Name="Second" 
              Content="Second" />
</StackPanel>

Storyboard可以存储在参考资料中以提高可读性,如下所示:

<Window.Resources>
    <Storyboard x:Key="FirstCheckedStory" ... />            
</Window.Resources>

然后像这样使用:

<EventTrigger RoutedEvent="CheckBox.Checked" SourceName="First">
    <BeginStoryboard Storyboard="{StaticResource FirstCheckedStory}" />
</EventTrigger>

此外,Storyboard可以包含多个操作,只需按顺序排列:

<Storyboard>
    <BooleanAnimationUsingKeyFrames ... />
    <BooleanAnimationUsingKeyFrames ... />
</Storyboard>