我的窗口代码定义了一个依赖属性,“活动”......
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }
public bool Active
{
get { return (bool) GetValue(ActiveProperty); }
set { SetValue(ActiveProperty, value); }
}
public static readonly DependencyProperty ActiveProperty =
DependencyProperty.Register("Active", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false));
}
然后我使用 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>
选中一个框会自动检查另一个框,但不会更改矩形颜色:(