Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将xaml中的datatrigger绑定到代码定义的依赖属性?_C#_Xaml_Data Binding_Datatrigger - Fatal编程技术网

C# 如何将xaml中的datatrigger绑定到代码定义的依赖属性?

C# 如何将xaml中的datatrigger绑定到代码定义的依赖属性?,c#,xaml,data-binding,datatrigger,C#,Xaml,Data Binding,Datatrigger,我的窗口隐藏代码定义了一个依赖属性“Active” 然后我使用xaml中的两个复选框绑定到该属性。我还希望基于该属性更改矩形的填充。我怎样才能做到这一点 <Window x:Class="WpfTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xa

我的窗口隐藏代码定义了一个依赖属性“Active”

然后我使用xaml中的两个复选框绑定到该属性。我还希望基于该属性更改矩形的填充。我怎样才能做到这一点

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
  <StackPanel>
    <CheckBox IsChecked="{Binding Active}" />
    <CheckBox IsChecked="{Binding Active}" />
    <Rectangle Fill="Gray"
               Width="50"
               Height="50">
      <Rectangle.Style>
        <Style TargetType="Rectangle">
          <Style.Triggers>
            <DataTrigger Binding="{Binding Active}"
                         Value="True">
              <Setter Property="Fill"
                      Value="Green" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </Rectangle.Style>
    </Rectangle>
  </StackPanel>
</Window>


选中一个框会自动选中另一个框,但不会更改矩形的颜色:(

一旦设置了一个属性,通过样式在该属性上定义的触发器将不再工作。您可以定义另一个dataTrigger来更改假值上的背景:

  <StackPanel>
        <CheckBox IsChecked="{Binding Active}"/>
        <CheckBox IsChecked="{Binding Active}"/>
        <Rectangle Width="50" Height="50">
            <Rectangle.Style>
                <Style TargetType="Rectangle">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Active}" Value="True">
                            <Setter Property="Fill" Value="Green"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Active}" Value="false">
                            <Setter Property="Fill" Value="Gray"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Rectangle.Style>
        </Rectangle>
    </StackPanel>

本地设置的属性始终覆盖样式集属性,因此您需要删除本地设置的属性,并在样式中设置默认值:

<Rectangle Width="50" Height="50">
    <Rectangle.Style>
        <Style TargetType="Rectangle">
            <Setter Property="Fill" Value="Gray" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Active}" Value="True">
                    <Setter Property="Fill" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>

我接受了另一个答案,因为它的语法更快、更简洁,但这个答案同样有效,所以请接受我谦逊的+1。
<Rectangle Width="50" Height="50">
    <Rectangle.Style>
        <Style TargetType="Rectangle">
            <Setter Property="Fill" Value="Gray" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Active}" Value="True">
                    <Setter Property="Fill" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>