在 WPF 中的数据绑定组合框中禁用分隔符选择
我有一个数据绑定的组合框。在此列表中,我需要一个分隔符。由于这是数据绑定,我做了一些与 这篇文章。我的数据库返回列表,包括一个“-”来标记分隔符需要去的位置,并且数据触发器使其成为分隔符。
<ComboBox Name="cbAction" Grid.Column="1" Grid.Row="0" Margin="5,2,5,2" DisplayMemberPath="Description" SelectedValuePath="Code" SelectionChanged="cbAction_SelectionChanged">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Code}" Value="-">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
除了我在这里遇到的问题和一个小设计问题(我将在另一个问题中提出)之外,这基本上工作得很好。
使用鼠标时,用户无法选择分隔符,这是正确的。但如果用户使用向上/向下箭头来选择项目,他们可以选择分隔符。这不是默认行为,默认行为会跳过分隔符。
如何使此分隔符的行为类似于 XAML 具有各种 ComboBoxItems 和分隔符项(使用向上和向下键时跳过分隔符)的方式
I have a Combo Box that is databound. In this list, I need a separator. Since this is databound, I do something very similar to this post. My database returns the list, includes a '-' to mark where the separator needs to go, and the datatrigger makes this a separator.
<ComboBox Name="cbAction" Grid.Column="1" Grid.Row="0" Margin="5,2,5,2" DisplayMemberPath="Description" SelectedValuePath="Code" SelectionChanged="cbAction_SelectionChanged">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Code}" Value="-">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
This works mostly fine, other than the issue I have here, and a minor design problem (which I will put in another question).
When using the mouse, the user can not select the separator, which is correct. But if the user uses the up/down arrow to select items, they can select the separator. This is not the default behavior, which would skip over the separator.
How can I make this separator behave similar to the way it would be if your XAML had various ComboBoxItems and a Separator item (skipping over the separator when using the up and down keys)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要按照 Meleak 的建议设置“Focusable”,而是在 Setter 中将“IsEnabled”设置为 false。
Instead of setting "Focusable" as suggested by Meleak, set "IsEnabled" to false instead in the Setter.
我尝试了上面提到的建议,但仍然无法获得分隔符。相反,它在组合框中添加了一个空白的可选条目。最后这对我有用。
我将绑定的数据项设置为NULL。我的 XAML 看起来是这样的:
I tried the suggestion mentioned above and I was still not able to get the separator. Instead it added a blank selectable entry in the combo box. Finally this is what worked for me.
I set the bound data item as NULL. And my XAML looks so:
可选择的项目不是分隔符本身,而是包含分隔符的 ComboBoxItem。
尝试在 DataTrigger 中设置 Focusable="False"。这应该使 ComboBoxItem “不可选择”
更新
固定二传位置
The selectable item is not the Separator itself but the ComboBoxItem containing a Separator.
Try to set Focusable="False" in the DataTrigger. This should make the ComboBoxItem "unselectable"
Update
Fixed Setter position