C# 禁用的单选按钮仍可选择
我有一个C# 禁用的单选按钮仍可选择,c#,wpf,radio-button,C#,Wpf,Radio Button,我有一个列表框,其中包含由单选按钮组成的项目。它们是通过为其提供ItemsSource动态创建的。以下是列表框的XAML: <ListBox Margin="20,5,0,0" Width="Auto" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" DisplayMemberPath="DisplayValue" SelectedValue
列表框
,其中包含由单选按钮组成的项目
。它们是通过为其提供ItemsSource
动态创建的。以下是列表框的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
禁用。这是有效的;在应用程序中,它被正确禁用。但是,如果用户单击disabled(已禁用)选项,它仍然允许选择该选项。我还尝试在父级边框上使用相同的转换器(如上面的xaml所示),但是行为没有改变
我还尝试为单选按钮
在单击事件上添加一个侦听器,如果他们选择了禁用选项,则将其设置为已处理,但由于某种原因该事件不会被触发
是否仍不允许用户选择此选项?如果不希望选择某个特定条目,可以在ListBoxItem样式中绑定该条目的IshittetVisible
像这样的,
<Setter Property="IsHitTestVisible" Value="{Binding IsSelectable}" />
其中IsSelectable是您的数据。您是否尝试在触发器中将IshitteVisible设置为false?太好了!使用转换器将禁用的单选按钮设置为false,谢谢!