我正在做这个小程序,只是为了学习如何将一些对象的数据绑定到组合框。我想要做的是在文本块中显示组合框中某些单词的翻译。在组合框中,我想要英文单词,例如在文本块西班牙语中。Combobox WPF list
对于我创建XAML中称为cmbBox1组合框和文本块称为TB1。
然后,我创建的类 “词”:
和列表的三个词:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
// Creation of a list of objects of class "word"
List<word> mydictionary = new List<word>();
word word1 = new word();
word1.english = "Hello";
word1.spanish = "Hola";
word word2 = new word();
word2.english = "Goodbye";
word2.spanish = "Adios";
word word3 = new word();
word3.english = "How are you?";
word3.spanish = "¿Qué tal?";
mydictionary.Add(word1);
mydictionary.Add(word2);
mydictionary.Add(word3);
//Adding the objects of the list mydictionary to combobox <---
foreach (word myword in mydictionary)
{
cmbBox1.Items.Add(myword);
}
}
和INT XAML我已经为我的组合框:
<ComboBox x:Name="cmbBox1" HorizontalAlignment="Left" Margin="133,122,0,0" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Path=word}"
DisplayMemberPath="english"
SelectedValuePath="english"
SelectedValue="{Binding Path=word}" />
我希望在组合框中显示属性“english”,并在文本块中显示属性“spanish”。如果用户在组合框中单击一个单词时会执行一个不同的方法,例如MessageBox.Show(“您选择了单词”+ word1.english“),这将会很不错。
所有这些的目的是学习如何做更复杂的事情:我将加载一些带有一些数据通道的文本文件,每个通道都会有一堆特性,我希望能够选择通道然后绘制它。非常感谢你。
非常感谢。你帮了我很多。 – Sturm 2013-05-11 12:29:47