0

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"/>

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

1
  • As Sam suggested, doing this in XAML only is perfectly possible and a lot easier with Triggers. Also, you shouldn't put code in the CLR setters of DependencyProperties... That's not how they work. And you don't need to Notify changes of a DependencyProperty since they already do that on their own. Commented Jun 11, 2015 at 10:26

1 Answer 1

2

Here how to do it entirely in Xaml using 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>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi,ThanksI actually need to use the dependency property object. COuld you please help me fix the code I have ? tanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.