2013-02-27 30 views
9

Tôi đang cố gắng điền một ComboBox với một cặp Chuỗi, Giá trị. Tôi đã làm nó trong mã đằng sau như thế này:Cách điền một ComboBox vào 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; 
    } 
} 

Như bạn thấy, tôi chèn giá trị Text sử dụng ResourceDictionary tôi. Nhưng nếu tôi làm theo cách này, khi tôi thay đổi ngôn ngữ khi chạy, nội dung ComboBox thì không.

Vì vậy, tôi muốn thử điền vào ComboBox của mình tại thiết kế (tại XAML).

Vì vậy, câu hỏi của tôi là: làm cách nào để điền vào ComboBox của mình với một cặp Văn bản, Giá trị như trên?

Trả lời

14

Bạn sẽ sử dụng Tag, không phải Value trong xaml. Điều này sẽ như sau:

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

Đó chính xác là giải pháp! Cảm ơn! :) – Sonhja

Các vấn đề liên quan