如何在 WPF ComboBox 中为下拉列表值/所选项目显示不同的值? [英] How to display a different value for dropdown list values/selected item in a WPF ComboBox?
问题描述
我有一个 WPF 组合框绑定到具有长描述的项目列表.
I have a WPF combobox bound to a list of items with long descriptions.
绑定到 ComboBox 的类型具有作为属性的短描述和长描述.目前,我对完整描述具有约束力.
The type bound to the ComboBox has both short and long description as properties. Currently, I am binding to the full description.
comboBox.DisplayMemberPath = "FullDescription";
如何确保当项目被选中并在组合框中显示为单个项目时,它将显示为ShortDescription
属性的值,而下拉列表将显示FullDescription代码>?
How to ensure that when the item is selected and displayed as a single item in the combobox, it will be displayed as a value of the ShortDescription
property while the dropdown will display FullDescription
?
推荐答案
Update 2011-11-14
我最近再次遇到相同的要求,我对我在下面发布的解决方案不太满意.这是一种无需重新模板化 ComboBoxItem
即可获得相同行为的更好方法.它使用 DataTemplateSelector
I recently came upon the same requirement again and I wasn't very happy with the solution I posted below. Here is a nicer way to get the same behavior without re-templating the ComboBoxItem
. It uses a DataTemplateSelector
首先,为ComboBox
指定资源中的常规DataTemplate
、下拉DataTemplate
和ComboBoxItemTemplateSelector
.然后将 ComboBoxItemTemplateSelector
引用为 ItemTemplateSelector
First, specify the regular DataTemplate
, the dropdown DataTemplate
and the ComboBoxItemTemplateSelector
in the resources for the ComboBox
. Then reference the ComboBoxItemTemplateSelector
as a DynamicResource
for ItemTemplateSelector
<ComboBox ...
ItemTemplateSelector="{DynamicResource itemTemplateSelector}">
<ComboBox.Resources>
<DataTemplate x:Key="selectedTemplate">
<TextBlock Text="{Binding Path=ShortDescription}"/>
</DataTemplate>
<DataTemplate x:Key="dropDownTemplate">
<TextBlock Text="{Binding Path=FullDescription}"/>
</DataTemplate>
<local:ComboBoxItemTemplateSelector
x:Key="itemTemplateSelector"
SelectedTemplate="{StaticResource selectedTemplate}"
DropDownTemplate="{StaticResource dropDownTemplate}"/>
</ComboBox.Resources>
</ComboBox>
ComboBoxItemTemplateSelector
检查容器是否是 ComboBoxItem
的子项,如果是,那么我们正在处理一个下拉项,否则就是 ComboBoxItem
的子项代码>组合框代码>.
ComboBoxItemTemplateSelector
checks if the container is the child of a ComboBoxItem
, if it is, then we are dealing with a dropdown item, otherwise it is the item in the ComboBox
.
public class ComboBoxItemTemplateSelector : DataTemplateSelector
{
public DataTemplate DropDownTemplate
{
get;
set;
}
public DataTemplate SelectedTemplate
{
get;
set;
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
ComboBoxItem comboBoxItem = VisualTreeHelpers.GetVisualParent<ComboBoxItem>(container);
if (comboBoxItem != null)
{
return DropDownTemplate;
}
return SelectedTemplate;
}
}
GetVisualParent
public static T GetVisualParent<T>(object childObject) where T : Visual
{
DependencyObject child = childObject as DependencyObject;
while ((child != null) && !(child is T))
{
child = VisualTreeHelper.GetParent(child);
}
return child as T;
}
<小时>
旧解决方案,需要重新模板化ComboBoxItem
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<ControlTemplate x:Key="FullDescriptionTemplate" TargetType="ComboBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<StackPanel>
<TextBlock Text="{Binding Path=FullDescription}"/>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ComboBox Name="c_comboBox" ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ShortDescription}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template" Value="{StaticResource FullDescriptionTemplate}" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
这会导致以下行为
这篇关于如何在 WPF ComboBox 中为下拉列表值/所选项目显示不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!