WPF RadioButtonList 从集合中设置 IsEnabled 和 IsChecked 属性

标签 wpf xaml wpf-controls isenabled

我已经重新设置了 ListBox 的样式,以便从此 link 创建一个 RadioButtonList

我的要求是这样的:我必须一次从 ListBox 中选择一个项目(即 单个 选择模式)。此外,我必须根据绑定(bind)到集合的属性禁用/启用 ListBoxItem。所以我设置了

IsChecked="{TemplateBinding IsSelected}"

并绑定(bind)已启用 我收藏的属性(property)。
IsEnabled="{Binding IsEnabled}" 

结果如下:
enter image description here

您可以看到一些记录处于禁用状态,但它们仍然是可选的。如果我删除 已检查 属性,它按预期完美运行。但我需要两个 已启用 & 已选中 功能性。
然后,我为 IsEnabled 属性创建了一个多值转换器,并根据我已将相应值绑定(bind)到该属性的值。
现在我无法从列表中直观地选择禁用的项目。但是当我选择一个禁用的项目时,我失去了选择。请检查图像:
enter image description here .

后面的代码中已检查 属性设置为第一条记录。
我想限制这个选择。我怎样才能做到这一点? xaml 中是否有任何设置有助于满足我的要求?请指教...

提前致谢....

最佳答案

听起来您正在绑定(bind) IsEnabled RadioButton 的属性(property)而不是 ListBoxItem .这将禁用 RadioButton ,但不是 ListBoxItem ,这就是为什么它仍然可以被选中。

您应该可以绑定(bind) IsEnabled ListBoxItem 的属性(property),它会按照你想要的方式工作。

根据您发布的指向您正在使用的样式的链接,该链接位于 ItemContainerStyleRadioButtonList 的部分风格:

<Style TargetType="{x:Type ListBoxItem}" >
    <!-- Here -->
    <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />

    <Setter Property="Margin" Value="5" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border BorderThickness="0" Background="Transparent">
                    <RadioButton Focusable="False"
                                 IsHitTestVisible="False"
                         IsChecked="{TemplateBinding IsSelected}">
                        <ContentPresenter />
                    </RadioButton>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

关于WPF RadioButtonList 从集合中设置 IsEnabled 和 IsChecked 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14938628/

相关文章:

.net - 将 WPF 样式应用于多个控件

wpf - 将 SelectedValue 绑定(bind)到 WPF 组合框

wpf - 按钮图像路径未在 wpf 中聚焦

c# - 删除 RichTextBox 中的双换行符到字符串

c# - 从范围中选择但排除某些数字

wpf - 从 ListBoxItem 访问 Storyboard(资源)

xaml - WinRT 中的当前文化是错误的

c# - ComboBox TwoWay 绑定(bind)不起作用

c# - WPF 列表框 IndexFromPoint

wpf - PowerShell WPF 数据网格 : Exception thrown committing empty row