【问题标题】:How to populate a ComboBox in XAML如何在 XAML 中填充组合框
【发布时间】:2013-02-27 18:23:32
【问题描述】:

我正在尝试用一对 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;

组合框项:

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

    public override string ToString()
    {
        return Text;
    }
}

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

所以我想尝试在设计(在 XAML 中)填写我的 ComboBox

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

【问题讨论】:

    标签: wpf xaml combobox


    【解决方案1】:

    您将在 xaml 中使用 Tag,而不是 Value。 应该是这样的:

    <ComboBox>
         <ComboBoxItem Tag="L" IsSelected="True">Low</ComboBoxItem>
         <ComboBoxItem Tag="H">High</ComboBoxItem>
         <ComboBoxItem Tag="M">Medium</ComboBoxItem>
    </ComboBox>
    

    【讨论】:

    • 这正是解决方案!谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 2020-03-14
    • 2015-03-23
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多