首页 文章

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

提问于
浏览
4

我重新设计了一个ListBox样式,以便从这个link创建一个RadioButtonList

我的要求是这样的:我必须一次从ListBox中选择一个项目(即 Single 选择模式) . 另外,我必须根据绑定到集合的属性禁用/启用ListBoxItem . 所以我已经确定了

IsChecked="{TemplateBinding IsSelected}"

并绑定我的集合中的 IsEnabled 属性 .

IsEnabled="{Binding IsEnabled}"

结果如下:
enter image description here

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

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

提前致谢....

1 回答

  • 1

    听起来你绑定 RadioButtonIsEnabled 属性而不是 ListBoxItem . 这将禁用 RadioButton ,但不会禁用 ListBoxItem ,这就是为什么它仍然可以被选中 .

    您应该能够绑定 ListBoxItemIsEnabled 属性,它将以您想要的方式工作 .

    根据您发布的链接转到您正在使用的样式,这将在 RadioButtonList 样式的 ItemContainerStyle 部分中:

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

相关问题