在 WPF 中的数据绑定组合框中禁用分隔符选择

发布于 2024-10-03 17:08:39 字数 1455 浏览 4 评论 0原文

我有一个数据绑定的组合框。在此列表中,我需要一个分隔符。由于这是数据绑定,我做了一些与 这篇文章。我的数据库返回列表,包括一个“-”来标记分隔符需要去的位置,并且数据触发器使其成为分隔符。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若沐 2024-10-10 17:08:39

不要按照 Meleak 的建议设置“Focusable”,而是在 Setter 中将“IsEnabled”设置为 false。

<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> 

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> 
心在旅行 2024-10-10 17:08:39

我尝试了上面提到的建议,但仍然无法获得分隔符。相反,它在组合框中添加了一个空白的可选条目。最后这对我有用。

我将绑定的数据项设置为NULL。我的 XAML 看起来是这样的:

<DataTrigger Binding="{Binding}" Value="{x:Null}">
    <Setter Property="IsEnabled" Value="False"/>
    <Setter Property="Template"> 
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
                <Separator HorizontalAlignment="Stretch"/> 
            </ControlTemplate> 
        </Setter.Value> 
    </Setter> 
</DataTrigger>

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:

<DataTrigger Binding="{Binding}" Value="{x:Null}">
    <Setter Property="IsEnabled" Value="False"/>
    <Setter Property="Template"> 
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
                <Separator HorizontalAlignment="Stretch"/> 
            </ControlTemplate> 
        </Setter.Value> 
    </Setter> 
</DataTrigger>
困倦 2024-10-10 17:08:39

可选择的项目不是分隔符本身,而是包含分隔符的 ComboBoxItem。
尝试在 DataTrigger 中设置 Focusable="False"。这应该使 ComboBoxItem “不可选择”

更新
固定二传位置

<DataTrigger Binding="{Binding Code}" Value="-"> 
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Template"> 
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
                <Separator HorizontalAlignment="Stretch" IsEnabled="False"/> 
            </ControlTemplate> 
        </Setter.Value> 
    </Setter> 
</DataTrigger> 

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

<DataTrigger Binding="{Binding Code}" Value="-"> 
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Template"> 
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
                <Separator HorizontalAlignment="Stretch" IsEnabled="False"/> 
            </ControlTemplate> 
        </Setter.Value> 
    </Setter> 
</DataTrigger> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文