如何在XAML中填充ComboBox

10

我正在尝试用一对String, Value填充ComboBox。我在后台代码中这样做:

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;
    }
}

正如您所见,我正在使用我的ResourceDictionary插入Text值。但是如果我以这种方式做,当我在运行时更改语言时,ComboBox的内容不会改变。

因此,我想尝试在设计时(在XAML中)填充我的ComboBox

所以我的问题是:如何像上面那样用一对文本、值填充我的ComboBox

1个回答

15
你需要在xaml中使用标签而不是。 这将像这样:
<ComboBox>
     <ComboBoxItem Tag="L" IsSelected="True">Low</ComboBoxItem>
     <ComboBoxItem Tag="H">High</ComboBoxItem>
     <ComboBoxItem Tag="M">Medium</ComboBoxItem>
</ComboBox>

那正是解决方案!谢谢! :) - Sonhja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接