我有一个 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 所示),但行为没有改变。
我还尝试在 RadioButton
的 Click
事件上添加一个监听器,以将其设置为在选择禁用选项时进行处理,但该事件不是由于某种原因被解雇。
是否有办法不允许用户选择此选项?
最佳答案
如果您希望不选择某个特定条目,您可以以 ListBoxItem 样式绑定(bind)该项目的 IsHitTestVisible。
类似这样的事情,
<Setter Property="IsHitTestVisible" Value="{Binding IsSelectable}" />
其中 IsSelectable 是您的数据。
关于c# - 禁用的RadioButton仍然可以选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23157227/