c# - 禁用的RadioButton仍然可以选择

标签 c# wpf radio-button

我有一个 ListBox,其中包含由 RadioButtons 组成的项目。它们是通过为其提供 ItemsSource 动态创建的。以下是 ListBox 的 XAML:

<ListBox 
   Margin="20,5,0,0" 
   Width="Auto" 
   Background="Transparent" 
   BorderThickness="0" 
   BorderBrush="Transparent" 
   DisplayMemberPath="DisplayValue" 
   SelectedValuePath="Value" 
   SelectedItem="{Binding Path=SingleAnswer}" 
   ItemsSource="{Binding Path=AnswerOptions}">
   <!-- KLG Score ItemsSource converter to filter out 'Unable to Assess' -->
   <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
   </ListBox.ItemsPanel>
   <ListBox.Resources>
      <Style TargetType="{x:Type ListBoxItem}">
         <Setter Property="Margin" Value="0,0,25,0" />
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type ListBoxItem}">
                  <Border Background="Transparent" BorderThickness="0" BorderBrush="Transparent">
                     <Border.IsEnabled>
                        <MultiBinding Converter="{StaticResource RadioButtonToEnabledConverter}" >
                           <Binding Mode="OneWay" ElementName="this" Path="ParentForm.ImagesExist"/>
                           <Binding Mode="OneWay" Path="." />
                        </MultiBinding>
                     </Border.IsEnabled>
                     <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Click="KLGScoreRadioButton_Click" >
                        <RadioButton.IsEnabled>
                           <MultiBinding Converter="{StaticResource RadioButtonToEnabledConverter}" >
                              <Binding Mode="OneWay" ElementName="this" Path="ParentForm.ImagesExist"/>
                              <Binding Mode="OneWay" Path="." />
                           </MultiBinding>
                        </RadioButton.IsEnabled>
                        <ContentPresenter />
                     </RadioButton>
                  </Border>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </ListBox.Resources>
</ListBox>

现在,用户不应选择其中一个答案,它只能根据业务逻辑指定的内容自动填充。

为此,我为 RadioButton.IsEnabled 属性创建了一个转换器,以始终将指定的 RadioButton 设置为禁用。这有效;在应用程序中它被正确禁用。但是,如果用户单击禁用的选项,仍然允许选择它。我还尝试在父 Border 上使用相同的转换器(如上面的 xaml 所示),但行为没有改变。

我还尝试在 RadioButtonClick 事件上添加一个监听器,以将其设置为在选择禁用选项时进行处理,但该事件不是由于某种原因被解雇。

是否有办法不允许用户选择此选项?

最佳答案

如果您希望不选择某个特定条目,您可以以 ListBoxItem 样式绑定(bind)该项目的 IsHitTestVisible。

类似这样的事情,

<Setter Property="IsHitTestVisible" Value="{Binding IsSelectable}" />

其中 IsSelectable 是您的数据。

关于c# - 禁用的RadioButton仍然可以选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23157227/

相关文章:

c# - 通过 Monogame 内容管道加载 XML 文件?

c# - 在 C# datacontract 上反序列化枚举时出错

c# - 打印到新页面的转义序列

wpf - VS2010 在设计时是否在 UserControls 中显示数据?

javascript - 通过 JavaScript 验证单选按钮

c# - 我可以让 mvc 模型验证对不同的编辑器使用不同的错误 css 样式吗?

C# dapper 映射到接口(interface)

javascript - 为什么在网络浏览器中用作字符串时失败说 d3 未定义

javascript - javascript 上的单选按钮验证

javascript - 水平和垂直独占单选按钮