如何在XAML中填充ComboBox

Son*_*hja 10 wpf xaml combobox

我正在尝试ComboBox用一对String,Value填充一个.我在代码背后做了这样的事情:

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;
Run Code Online (Sandbox Code Playgroud)

ComboBoxItem:

public class ComboBoxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在Text使用my 插入值ResourceDictionary.但是,如果我这样做,当我在运行时更改语言时,ComboBox内容不会.

所以我想尝试填充我ComboBox的设计(在XAML).

所以我的问题是:我怎样才能填充ComboBox一对Text,Value如上?

Far*_*yev 15

您将使用Tag,而不是Valuexaml.这将是这样的:

<ComboBox>
     <ComboBoxItem Tag="L" IsSelected="True">Low</ComboBoxItem>
     <ComboBoxItem Tag="H">High</ComboBoxItem>
     <ComboBoxItem Tag="M">Medium</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)