在WPF中的数据绑定组合框中禁用分隔符选择 [英] Disable separator selection in data bound combo box in WPF
问题描述
我有一个数据绑定的组合框。在这个列表中,我需要一个分隔符。由于这是数据绑定,我做一些非常类似于此信息。我的数据库返回列表,包括一个 - 标记分隔符需要去的地方,而数据挖掘机使它成为一个分隔符。
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.
我如何使这个分隔符的行为类似于如果你的XAML有各种ComboBoxItems和一个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)
推荐答案
除了设置Meleak建议的Focusable,设置IsEnabled / p>
Instead of setting "Focusable" as suggested by Meleak, set "IsEnabled" to false instead in the Setter.
<DataTrigger Binding="{Binding Code}" Value="-">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Separator HorizontalAlignment="Stretch"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
这篇关于在WPF中的数据绑定组合框中禁用分隔符选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!