2017-03-01 100 views
2

问题
我有一个列表框,在那个列表框中有复选框。在第一次点击时,复选框被选中并被选中。在第二次点击复选框只被设置。可以使用箭头键重新选择不同的复选框。我的目标是,首先选择复选框,然后再次检查(再次单击),从而不再需要箭头键。带有复选框的XAML列表框,选中复选框查看它

目标

  • 选择首先点击
  • 项目选中复选框上的第二次点击

守则

<ListBox Name="Terminals" ItemsSource="{Binding AllTerminals, Mode=OneWay}" IsSynchronizedWithCurrentItem="True"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Content="{Binding Description}" Foreground="{Binding DisplayColor}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

回答

3

无效点击registra通过IsHitTestVisible属性绑定到一个ListBoxItem的选择状态的复选框重刑:

<ListBox Name="Terminals" ItemsSource="{Binding AllTerminals, Mode=OneWay}" IsSynchronizedWithCurrentItem="True"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Content="{Binding Description}" 
         IsHitTestVisible="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" 
         Foreground="{Binding DisplayColor}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

这样,第一次点击将只选择一个ListBoxItem和复选框可以添加物品后进行检查/通过第二次点击


选中到列表框,视觉树如下:

ListBox 
--ListBoxItem 
    --CheckBox 
--ListBoxItem 
    --CheckBox 
--ListBoxItem 
    --CheckBox 

当ListBoxItem的用户点击,它就会被选中(IsSelected = TRUE)。当用户点击CheckBox时,它会被选中或取消选中。但如果IsHitTestVisible设置为false,则单击元素将不会被注册。由于check/uncheck只适用于选定的项目,因此我们可以在CheckBox.IsHitTestVisible和ParentBoxListItem.IsSelected之间创建绑定来达到这样的效果

+0

感谢您的快速回答我会试一试 –

+0

对我来说非常感谢很多:)如果你找到时间,你可以扩展它的工作方式,这样我就可以完全理解它吗? –

+1

@Nerdintraining,看到我的更新,我添加了它的工作原理 – ASh