trinadh
trinadh

Reputation: 137

checkbox disabling and enabling in wpf

IN my below code i have 2 check boxes chkQAReviewMandatory and chkIsAnsMandatory. My requirement is when i check the chkQAReviewMandatory checkbox then automatically i need to checkbox chkIsAnsMandatory is checked and disabled, If chkQAReviewMandatory is not checked then uncheck the chkIsAnsMandatory and also enable it.

      <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal" Margin="5,15,5,0">
                        <CheckBox x:Name="chkQAReviewMandatory" Content="QA Review Mandatory" Grid.Row="1" Grid.Column="0" Margin="0,5,0,0" 
                                  IsChecked="{Binding IsQAReviewMandatory}"/>
                        <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                        <StackPanel.Style>
                            <Style TargetType="StackPanel">
                            <Setter Property="Visibility" Value="Hidden"/>
                            <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=chkQAReviewMandatory, Path=IsChecked}" Value="True">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                        </StackPanel.Style>
                                <Label x:Name="lblMax" Content="QA Max Point" Height="20" VerticalAlignment="Center" Margin="0,5,5,0"></Label>
                                <local:NumericTextBox Width="50"
                                                             MaxLength="1"
                                                             Height="25"
                                                             VerticalAlignment="Top"
                                                             Text="{Binding MaximumQAPoints}" />
                    </StackPanel>
                    </StackPanel>

                     <CheckBox Content="Is Answer Mandatory" 
                          Grid.Row="2" 
                          HorizontalAlignment="Left" 
                          Margin="5,12,0,0" 
                          IsChecked="{Binding IsAnswerMandatory}" >
                        <CheckBox.Style>
                            <Style TargetType="CheckBox">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=chkQAReviewMandatory,Path=IsChecked}" Value="True">
                                        <Setter Property="IsChecked" Value="True"/>
                                        <Setter Property="IsEnabled" Value="False"/>
                                        <Setter Property="Foreground" Value="White"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ElementName=chkQAReviewMandatory,Path=IsChecked}" Value="False">
                                        <Setter Property="IsChecked" Value="False"/>
                                        <Setter Property="IsEnabled" Value="True"/>
                                        <Setter Property="Foreground" Value="White"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </CheckBox.Style>
                    </CheckBox>

Upvotes: 1

Views: 5229

Answers (1)

Contango
Contango

Reputation: 80398

There are two ways:

  1. For one control, bind the IsEnabled and IsChecked properties to properties in the ViewModel. On the other control, bind the IsEnabled to the IsChecked. This means that if one checkbox gets ticked, the other one becomes enabled.
  2. You can not only bind to properties in code, you can bind to properties on other controls. Set the name of the control using x:Name, then you can bind to any property on that control.

As for the details on how to do this, use my answer to find keywords to Google. Good luck!

Upvotes: 2

Related Questions