2013-02-27 101 views
9

我試圖用一對字符串填充ComboBox,值爲。我做到了,在後面的代碼是這樣的:如何在XAML中填充組合框

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

正如你所看到的,我將用我的ResourceDictionaryText值。但是如果我這樣做,當我在運行時更改語言時,ComboBox內容不會。

所以我想嘗試填寫我的ComboBox在設計(在XAML)。

所以我的問題是:如何填寫我的ComboBox一對文本,價值像上面?

回答

14

您將在xaml中使用Tag而不是Value。 這將是這樣的:

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

這正是解決方案!謝謝! :) – Sonhja 2013-02-28 08:33:16