C# WPF ComboBox...如何设置 .Text 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/895014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 21:55:51  来源:igfitidea点击:

WPF ComboBox...how to set the .Text property?

c#wpfvisual-studio-2008combobox

提问by AngryHacker

Is there a way to set the .Textproperty of the Wpf ComboBox control directly?

有没有办法直接设置.TextWpf ComboBox控件的属性?

My combobox is bound to a List<T>collection, but when I try to set .Textproperty in the DropDownClosedevent, it totally ignores it.

我的组合框绑定到一个List<T>集合,但是当我尝试.TextDropDownClosed事件中设置属性时,它完全忽略了它。

采纳答案by Ray

If you're trying to set the text to the text of one of the items in the list it should work correctly.

如果您尝试将文本设置为列表中某一项的文本,它应该可以正常工作。

If you're trying to set it to an item that isn't in the list then the IsEditable property needs to be true. You can set the IsReadOnly property to true if you don't actually want users typing into the combo box.

如果您尝试将其设置为不在列表中的项目,则 IsEditable 属性需要为 true。如果您实际上不希望用户在组合框中键入内容,则可以将 IsReadOnly 属性设置为 true。

If none of these cases fit you, can you post the code you're using?

如果这些情况都不适合您,您可以发布您正在使用的代码吗?

回答by ari k

You can also try adding one more ComboBoxItem with Visibility=Collapsed, set that one as the selected item and set its content to whatever is needed.

您还可以尝试添加一个 Visibility=Collapsed 的 ComboBoxItem,将其设置为所选项目并将其内容设置为所需的任何内容。