带有标题的多个项目组合框?

编程入门 行业动态 更新时间:2024-10-28 12:19:50
本文介绍了带有标题的多个项目组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在绑定到多个项目的组合框中设置列标题"?例如,显示人员姓名的组合框.组合框将显示 John Doe.但我想显示列标题:

Is it possible to have "column headers" on a combo box bound to multiple items? For example a combo box that displays a persons name. The combo box would display John Doe. But I'd like to display column headers:

First Last John Doe Jane Doe Jimmy Doe

如果不使用数据网格,这可能吗?包含使用数据网格的简单解决方案怎么样?我找到了一种将数据网格嵌入组合框的解决方案,但它看起来很困难并且需要 MS Blend.

Is this possible without the use of a data grid? What about a simple solution that includes the use of a data grid? I found one solution for embedding a data grid into a combo box but it looks difficult and requires MS Blend.

如果我能在下拉列表的第一行中获得一些标题,我会很高兴.

I'd be happy if I could just get some headers as the first row in the drop down.

G

这是我的 xaml 代码,其中 HB 尝试产生了注释中提到的编译错误.

Here is my xaml code with HB's attempt that produces a compile error as mentioned in the comments.

xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:dg="schemas.microsoft/wpf/2008/toolkit"

<ComboBox Name="cboPlaceNames" Grid.IsSharedSizeScope="True" ItemsSource="{DynamicResource items}" Height="22" Width="285" Margin="0,6,165,0" SelectedIndex="0" HorizontalAlignment="Right" VerticalAlignment="Top" SelectionChanged="cboPlaceNames_SelectionChanged"> <ComboBox.Resources> <CompositeCollection x:Key="items"> <ComboBoxItem IsEnabled="False"> <Grid TextElement.FontWeight="Bold"> <Grid.ColumnDefinitions> <ColumnDefinition SharedSizeGroup="A"/> <ColumnDefinition Width="5"/> <ColumnDefinition SharedSizeGroup="B"/> <ColumnDefinition Width="5"/> <ColumnDefinition SharedSizeGroup="C"/> </Grid.ColumnDefinitions> <Grid.Children> <TextBlock Grid.Column="0" Text="Name"/> <TextBlock Grid.Column="2" Text="CLLI"/> <TextBlock Grid.Column="4" Text="Street"/> </Grid.Children> </Grid> </ComboBoxItem> <Separator/> <CollectionContainer Collection="{Binding Source={x:Reference cboPlaceNames}, Path=DataContext.Data}"/> </CompositeCollection> <DataTemplate DataType="x:Type obj:PlaceName"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition SharedSizeGroup="A"/> <ColumnDefinition Width="5"/> <ColumnDefinition SharedSizeGroup="B"/> <ColumnDefinition Width="5"/> <ColumnDefinition SharedSizeGroup="C"/> </Grid.ColumnDefinitions> <Grid.Children> <TextBlock Grid.Column="0" Text="{Binding Name}"/> <TextBlock Grid.Column="2" Text="{Binding CLLI}"/> <TextBlock Grid.Column="4" Text="{Binding Street}"/> </Grid.Children> </Grid> </DataTemplate> </ComboBox.Resources> </ComboBox>

推荐答案

示例:

<ComboBox Name="cb" Grid.IsSharedSizeScope="True" ItemsSource="{DynamicResource items}"> <ComboBox.Resources> <CompositeCollection x:Key="items"> <ComboBoxItem IsEnabled="False"> <Grid TextElement.FontWeight="Bold"> <Grid.ColumnDefinitions> <ColumnDefinition SharedSizeGroup="A"/> <ColumnDefinition Width="5"/> <ColumnDefinition SharedSizeGroup="B"/> </Grid.ColumnDefinitions> <Grid.Children> <TextBlock Grid.Column="0" Text="Name"/> <TextBlock Grid.Column="2" Text="Occupation"/> </Grid.Children> </Grid> </ComboBoxItem> <Separator/> <CollectionContainer Collection="{Binding Source={x:Reference cb}, Path=DataContext.Data}"/> </CompositeCollection> <DataTemplate DataType="{x:Type obj:Employee}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition SharedSizeGroup="A"/> <ColumnDefinition Width="5"/> <ColumnDefinition SharedSizeGroup="B"/> </Grid.ColumnDefinitions> <Grid.Children> <TextBlock Grid.Column="0" Text="{Binding Name}"/> <TextBlock Grid.Column="2" Text="{Binding Occupation}"/> </Grid.Children> </Grid> </DataTemplate> </ComboBox.Resources> </ComboBox>

请注意,获得 Collection 绑定权限并不是那么容易,因为既没有 DataContext 也没有 VisualTree 可以依赖,ElementName 和 RelativeSource 不起作用,这是因为 CompositeCollection 只是一个集合,而不是 FrameworkElement.

Note that getting the Collection-binding right is not that easy because there is neither DataContext nor VisualTree to rely on, ElementName and RelativeSource does not work, this is because CompositeCollection is just a collection, not a FrameworkElement.

除此之外,这样做的方式是通过具有共享大小列的网格.DataTemplate 通过 DataType 自动应用.

Other than that the way this is done is via Grids that have shared size columns. The DataTemplate is applied automatically via the DataType.

将 header-ComboBoxItem 的 IsHitTestVisible 属性设置为 False 是不够的,因为它仍然可以使用键盘进行选择.我现在将其更改为 IsEnabled="False" ,它会稍微淡出该项目.您可能可以重新模板该项目以不这样做.或者,如果您找到另一种方法来禁用它,当然也可以.

Setting the header-ComboBoxItem's IsHitTestVisible property to False is not enough since it still can be selected using the keyboard. I now changed it to IsEnabled="False" which fades out the item a bit. You could probably re-template that item to not do that. Or if you find another way of disabling it from selection that would of course work out too.

更多推荐

带有标题的多个项目组合框?

本文发布于:2023-11-05 15:31:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   多个   标题   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!