2014-04-18 70 views
1

我有一个ListBox,其中包含由RadioButtons组成的项目。它们是通过为其提供ItemsSource动态创建的。下面是ListBox的XAML:已禁用RadioButton仍然可以选择

<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所示),但行为不变。

我也试过在的Click事件中添加一个侦听器,如果它们选择禁用选项,将其设置为处理,但该事件由于某种原因未被触发。

无论如何不允许用户选择此选项吗?

+0

为什么downvote ...? – Saggio

+0

您是否尝试在触发器中将IsHitTestVisible设置为false? – whoisthis

回答

3

如果您希望不选择特定条目,则可以在ListBoxItem样式中绑定该项目的IsHitTestVisible。

这样的事情,

<Setter Property="IsHitTestVisible" Value="{Binding IsSelectable}" /> 

其中IsSelectable是您的数据。

+0

完美!使用转换器将'IsHitTestVisible'设置为false,以禁用'RadioButton',谢谢! – Saggio

相关问题