【问题标题】:checkbox disabling and enabling in wpfwpf中的复选框禁用和启用
【发布时间】:2015-06-24 16:29:16
【问题描述】:

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

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

【问题讨论】:

  • 您有问题吗?
  • 以上是一个问题
  • 我的问题是当我选中复选框“chkQAReviewMandatory”然后我想禁用复选框“chkIsAnsMandatory”选中选项

标签: wpf xaml mvvm


【解决方案1】:

有两种方式:

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

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

【讨论】:

  • 嗨,我想通过使用触发器来做到这一点
  • 嗨,我使用数据触发器编辑了上述更改的答案,一切正常,但 ischecked 属性在触发器中不起作用...
猜你喜欢
  • 2019-03-31
  • 2013-10-11
  • 1970-01-01
  • 2019-03-04
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多