如何在 WPF ComboBox 中为下拉列表值/所选项目显示不同的值?
我有一个 WPF 组合框,绑定到具有长描述的项目列表。
绑定到 ComboBox 的类型具有短描述和长描述作为属性。目前,我受完整描述的约束。
comboBox.DisplayMemberPath = "FullDescription";
如何确保当选择项目并在组合框中显示为单个项目时,它将显示为 ShortDescription
属性的值,而下拉列表将显示 FullDescription
?
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";
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
仅当
IsEditable
为 false 时,接受的解决方案才有效。如果
IsEditable
为 true,即,如果该控件是组合列表和自由输入文本框意义上的“真正的”组合框,则有一个非常简单的解决方案:请注意,这是有效的即使
IsTextSearchEnable
为 false。The accepted solution only works if
IsEditable
is false.If
IsEditable
is true, i.e., if the control is a "real" combo box in the sense of combining a list and a free-input text box, there is a really simple solution:Note that this works even if
IsTextSearchEnable
is false.我发现的另一个选择是在组合框文本区域上放置一个文本框。调整大小并对齐它,使其完美地放在上面,然后使用与此类似的子:(
将 * 替换为相关数字)
结果是,当选择时,下拉列表将像往常一样显示列表,但位于其上方的文本框将仅显示其第一个字符。
希望这有帮助。
Another option I have found is to place a textbox over the combobox text area. Size and align it so that it lays perfectly over it then use a sub similar to this:
(replace the * with the relevant numbers)
the result is that when selected the dropdown will display the list as usual but the textbox lying over it will only show its first character.
Hope this helps.
更新 2011-11-14
我最近再次遇到相同的要求,但我对下面发布的解决方案不太满意。这是一种更好的方法,无需重新模板化
ComboBoxItem
即可获得相同的行为。它使用DataTemplateSelector
首先,在资源中指定常规
DataTemplate
、下拉列表DataTemplate
和ComboBoxItemTemplateSelector
组合框
。然后将ComboBoxItemTemplateSelector
引用为ItemTemplateSelector
的DynamicResource
ComboBoxItemTemplateSelector
检查容器是否是的子级ComboBoxItem
,如果是,那么我们正在处理一个下拉项,否则它是ComboBox
中的项。GetVisualParent
旧解决方案,需要重新模板化
ComboBoxItem
这会导致以下行为
Update 2011-11-14
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 aDataTemplateSelector
First, specify the regular
DataTemplate
, the dropdownDataTemplate
and theComboBoxItemTemplateSelector
in the resources for theComboBox
. Then reference theComboBoxItemTemplateSelector
as aDynamicResource
forItemTemplateSelector
ComboBoxItemTemplateSelector
checks if the container is the child of aComboBoxItem
, if it is, then we are dealing with a dropdown item, otherwise it is the item in theComboBox
.GetVisualParent
Old solution, requires re-templating of
ComboBoxItem
This results in the following behavior
它现在似乎对我不起作用,但这个可以:
It doesn't seem to work for me now, but this one does:
我修改了这个自定义圆形 WPF ComboBox 以显示与所选项目不同的值,并更改每个项目的颜色。
自定义组合框
首先,您需要创建结构:
初始化结构,填充数据并绑定到 ComboBox:
现在将 ComboBox 放入您的设计中。要将其用作普通的 ComboBox,请删除 DisplayMemberPath 并将“ColorComboBoxItem”重命名为“CustomComboBoxItem”:
现在将以下样式/模板添加到 App.xaml Application.Resources:
我希望这会有所帮助..
I modified this custom rounded WPF ComboBox to display a different value from the item selected as well as change the color for each item.
Custom ComboBox
First you need to create the structure:
Initialize the structure, fill it with data and bind to ComboBox:
Now place the ComboBox in your design. To use it as a normal ComboBox, remove DisplayMemberPath and rename "ColorComboBoxItem" to "CustomComboBoxItem":
Now add the following styles/template to App.xaml Application.Resources:
I hope this helps..
该解决方案适用于 WPF + MVVM。
其他解决方案有些有效,有些则无效。其他一些解决方案的问题是,如果它们不起作用,有时很难调试它为什么不起作用,特别是如果没有使用 WPF 的经验。
在我看来,最好使用字符串进行绑定,并转换为 C# 中的枚举,这意味着一切都更容易排除故障。
您可能需要使用 ReSharper,它会自动建议任何缺少的命名空间。
创建一个带有描述属性的枚举:
和一个 ComboBox:
XAML 中的转换器需要指向 C# 类。如果您使用的是 UserControl 或 Window,则它将是 UserControl.Resources 或 Window.Resources。
在项目中的任何位置添加一些扩展方法和转换器:
在 ViewModel 中:
在构造函数中:
所选项目将绑定到此。它使用转换器从组合框中的字符串直接转换为枚举。您还可以使用上面的扩展方法在属性更新器内进行转换。
This solution is for WPF + MVVM.
Some of the other solutions work, and some of them do not. The problem with some other solutions are that if they do not work, it's sometimes difficult to debug why it is not working, especially if one is not experienced with WPF.
In my opinion, it's preferable to use strings for the bindings, and convert to an enum in C# which means everything is easier to troubleshoot.
You might need to use ReSharper, it will auto-suggest any missing namespaces.
Create an enum with description attributes:
And a ComboBox:
The converter in XAML needs to be pointed at the C# class. If you are using a UserControl or a Window, it would be
UserControl.Resources
orWindow.Resources
.Add some extension methods and a converter anywhere in your project:
In your ViewModel:
And in the constructor:
The selected item will be bound to this. It uses the converter to go from the string in the combobox straight to the enum. You could also do the conversion inside the property updater by using the extension methods above.