复选框在WPF中禁用和启用 [英] checkbox disabling and enabling in wpf

查看:106
本文介绍了复选框在WPF中禁用和启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我有2个复选框chkQAReviewMandatory和chkIsAnsMandatory.我的要求是,当我选中chkQAReviewMandatory复选框时,我需要自动选中chkIsAnsMandatory复选框并禁用它;如果未选中chkQAReviewMandatory,则取消选中chkIsAnsMandatory并启用它.

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>

推荐答案

有两种方法:

  1. 对于一个控件,将IsEnabledIsChecked属性绑定到ViewModel中的属性.在另一个控件上,将IsEnabled绑定到IsChecked.这意味着,如果选中了一个复选框,则另一个复选框将被启用.
  2. 您不仅可以绑定到代码中的属性,还可以绑定到其他控件上的属性.使用x:Name设置控件的名称,然后可以绑定到该控件上的任何属性.
  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.

有关如何执行此操作的详细信息,请使用我的答案来查找Google的关键字.祝你好运!

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

这篇关于复选框在WPF中禁用和启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆