Problem:
我有一个列表框,其中列表框是复选框。第一次单击时,复选框将被选中并选中。第二次单击时,仅设置复选框。人们可以使用箭头键重新选择不同的复选框。我的目标是,首先选择复选框,然后再检查(再次单击它),从而消除对箭头键的需要。
Goal:
The Code:
<ListBox Name="Terminals" ItemsSource="{Binding AllTerminals, Mode=OneWay}" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Description}" Foreground="{Binding DisplayColor}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
通过绑定禁用 CheckBox 上的点击注册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后,可视化树如下:
ListBox
--ListBoxItem
--CheckBox
--ListBoxItem
--CheckBox
--ListBoxItem
--CheckBox
当用户单击 ListBoxItem 时,它会被选中 (IsSelected=true)。当用户单击复选框时,它会被选中或取消选中。但如果IsHitTestVisible
设置为 false,点击元素将不会被注册。由于选中/取消选中仅适用于选定的项目,因此我们可以在 CheckBox.IsHitTestVisible 和父 ListBoxItem.IsSelected 之间创建绑定来实现这种效果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)