WPF。如果选中其他复选框,请选中并禁用复选框

时间:2015-06-11 09:53:58

标签: wpf checkbox binding

我有2个复选框。 选中一个时,必须检查并禁用另一个。 到目前为止我的代码是这样的

的Xaml

 <CheckBox x:Name="chkSABranches" Content="Apply to all branches" IsChecked="{Binding IsSABranches,ElementName=pgPageTemplate}" Grid.Row="0" Grid.Column="2"/>

在xaml.cs中:

 public static DependencyProperty IsSABranchesProperty = DependencyProperty.Register("IsSABranches", typeof(bool), typeof(pgAccounts), new PropertyMetadata(false));
        public static DependencyProperty IsSAWarehousesProperty = DependencyProperty.Register("IsSAWarehouses", typeof(bool), typeof(pgAccounts), new PropertyMetadata(false));

  public Boolean IsSABranches
        {
            get
            {
                return (bool)GetValue(IsSABranchesProperty);
            }
            set
            {
                SetValue(IsSABranchesProperty, value);
                NotifyPropertyChanged("IsSABranches");
                NotifyPropertyChanged("IsSAWarehouses");
            }
        }

 public Boolean IsSAWarehouses
        {
            get
            {
                return (bool)GetValue(IsSAWarehousesProperty) || (bool)GetValue(IsSABranchesProperty);
            }
            set
            {
                SetValue(IsSAWarehousesProperty, value);
                NotifyPropertyChanged("IsSAWarehouses");
            }
        }

这似乎不起作用..任何人都可以提供一些指导。感谢

1 个答案:

答案 0 :(得分:2)

以下是如何使用Xaml

完全在DataTrigers中完成的操作
<StackPanel>
    <CheckBox x:Name="CheckBox1"></CheckBox>
    <CheckBox >
        <CheckBox.Style>
            <Style TargetType="CheckBox">
                <Setter Property="IsEnabled" Value="True"></Setter>
                <Setter Property="IsChecked" Value="False"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked,ElementName=CheckBox1}" Value="True">
                        <Setter Property="IsChecked" Value="True"/>
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </CheckBox.Style>
    </CheckBox>

</StackPanel>