2013-02-18 112 views
4

我已经重新风格的列表框,从这个linkWPF RadioButtonList的同时设置的IsEnabled和器isChecked从集合

我的要求创建一个单选按钮列表属性是这样的:我在来从列表框中选择一个项目一段时间(即单人选择模式)。此外,我必须禁用/启用基于绑定到集合的属性的ListBoxItem。所以我已经设置

IsChecked="{TemplateBinding IsSelected}" 

从我收集绑定的的IsEnabled财产。

IsEnabled="{Binding IsEnabled}" 

而结果如下: enter image description here

你可以看到,一些记录是禁用状态,但他们仍然是selectable.If我删除器isChecked属性,它可以完美如预期。但我同时需要IsEnabled & IsSelected功能。 然后,我为IsEnabled属性创建一个多值转换器,并基于这些值将相应的值绑定到属性。 现在我无法从列表中直观地选择禁用的项目。但是,当我选择一个禁用的项目,我失去了选择。请检查图像: enter image description here

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

感谢....

回答

1

这听起来像你绑定RadioButton而不是ListBoxItemIsEnabled财产。这将禁用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>