I'm doing this little program just to learn how to bind some data from a list of objects to a combobox. What I want to do is to show in a textblock the translation of some words that are in the combobox. In the combobox I want the english word, and in the textblock spanish for example.
For that I created a combobox in xaml called cmbBox1 and a textblock called tb1.
Then I created the class "word":
public class word
{
public string english { get; set; }
public string spanish { get; set; }
}
And the lists with three words:
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);
}
}
And int XAML i have this for my combobox:
<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}" />
I would like to have the property "english" displayed in the combobox and the property "spanish" in the textblock. It would be nice too if when the user click on a word in the combobox a different method is executed, for example MessageBox.Show("You selected the word" + word1.english).
The purpose of all this is to learn how to do something more complex: I will be loading a text files with some channels of data, each channel will have a bunch of propreties and I want to be able to select the channel for then plotting it. Thank you very much.