如何显示下拉列表中的值/在WPF组合框所选项目的不同的价值?
我必然要长描述的项目列表的WPF组合框。
I have a WPF combobox bound to a list of items with long descriptions.
绑定到该组合框的类型有短期和长期的描述作为属性。目前,我绑定到完整的描述。
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
?
的 更新2011-11-14 的
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
首先,指定普通的的DataTemplate
,下拉的DataTemplate
和 ComboBoxItemTemplateSelector
中的组合框
的资源。然后引用 ComboBoxItemTemplateSelector
为 DynamicResource
为 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
,如果是这样,那么我们正在处理的孩子一个下拉的项目,否则它是在组合框的项目
。
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>
这将导致以下行为