请选中并禁用复选框

请选中并禁用复选框

本文介绍了WPF。如果选中其他复选框,请选中并禁用复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



Xaml

I have 2 checkboxes. when one is checked, the other one must be checked and disabled. The code I have so far is like so

Xaml

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







在xaml.cs:




In 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");
            }
        }



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


This doesn't seem to work.. Could anyone please provide some guidance. thanks

推荐答案

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

           }
       }

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







要禁用其他复选框,eighter写下转换器或另一个包装属性返回IsSABranches的否定值。



希望这可以解决您的问题。




To make other check box disabled, eighter write down converter or another wrapper property to return negated value of IsSABranches.

Hope this will solve your problem.


这篇关于WPF。如果选中其他复选框,请选中并禁用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:32