wpf 如何在 XAML 中填充 ComboBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15119435/
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
How to populate a ComboBox in XAML
提问by Sonhja
I'm trying to populate a ComboBoxwith a pair of String, Value. I did it in code behind like this:
我正在尝试ComboBox用一对String, Value填充 a 。我在后面的代码中这样做了:
listCombos = new List<ComboBoxItem>();
item = new ComboBoxItem { Text = Cultures.Resources.Off, Value = "Off" };
listCombos.Add(item);
item = new ComboBoxItem { Text = Cultures.Resources.Low, Value = "Low" };
listCombos.Add(item);
item = new ComboBoxItem { Text = Cultures.Resources.Medium, Value = "Medium" };
listCombos.Add(item);
item = new ComboBoxItem { Text = Cultures.Resources.High, Value = "High" };
listCombos.Add(item);
combo.ItemsSource = listCombos;
ComboBoxItem:
组合框项目:
public class ComboBoxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
As you can see, I'm inserting the Textvalue using my ResourceDictionary. But if I do it in this way, when I change language at runtime, the ComboBoxcontent doesn't.
如您所见,我正在Text使用我的ResourceDictionary. 但是如果我这样做,当我在运行时更改语言时,ComboBox内容不会。
So I wanted to try to fill my ComboBoxat the design (at XAML).
所以我想尝试填补我ComboBox的设计(在 XAML)。
So my question is: how can I fill my ComboBoxwith a pair Text, Valuelike above?
所以我的问题是:如何ComboBox用上面的一对Text, Value填充我的?
回答by Farhad Jabiyev
You will use Tag, not Valuein xaml.
This would be like this:
您将使用Tag, 而不是Value在 xaml 中。这将是这样的:
<ComboBox>
<ComboBoxItem Tag="L" IsSelected="True">Low</ComboBoxItem>
<ComboBoxItem Tag="H">High</ComboBoxItem>
<ComboBoxItem Tag="M">Medium</ComboBoxItem>
</ComboBox>

