Hello all, Here is my problem I am working on a homework assignment for class. I have the image control source set to
the flag image property of my country object. I have the ComboBox control binding to the name property of the Country
object.My country objects are in the same array and all is good now. Now the user is suppose to cycle threw the flags
and match them with the correct name in the ComboBox . My Problem is how do I get the country object that the image property belongs to, so I can check the name property of that object with the name property of the combobox object to check if the user has selected the correct country. I am sorry I hope I made this clear.Here is my code.Thanks.
the flag image property of my country object. I have the ComboBox control binding to the name property of the Country
object.My country objects are in the same array and all is good now. Now the user is suppose to cycle threw the flags
and match them with the correct name in the ComboBox . My Problem is how do I get the country object that the image property belongs to, so I can check the name property of that object with the name property of the combobox object to check if the user has selected the correct country. I am sorry I hope I made this clear.Here is my code.Thanks.
public partial class MainWindow : Window { Country[] countries = new Country[8]; public BitmapImage imgChina; public BitmapImage imgBrazil; public BitmapImage imgAustralia; public BitmapImage imgItaly; public BitmapImage imgRussia; public BitmapImage imgSouthAfrica; public BitmapImage imgSpain; public BitmapImage imgUnitedStates; int counter = 0; public MainWindow() { InitializeComponent(); countries[0] = new Country("Australia", imgAustralia = new BitmapImage(new Uri("pack://application:,,,/Images/australia.png"))); countries[1] = new Country("Brazil", imgBrazil = new BitmapImage(new Uri("pack://application:,,,/Images/brazil.png"))); countries[2] = new Country("China", imgChina = new BitmapImage(new Uri("pack://application:,,,/Images/china.png"))); countries[3] = new Country("Italy", imgItaly = new BitmapImage(new Uri("pack://application:,,,/Images/italy.png"))); countries[4] = new Country("Russia", imgRussia = new BitmapImage(new Uri("pack://application:,,,/Images/russia.png"))); countries[5] = new Country("South Africa", imgSouthAfrica = new BitmapImage(new Uri("pack://application:,,,/Images/southafrica.png"))); countries[6] = new Country("Spain", imgSpain = new BitmapImage(new Uri("pack://application:,,,/Images/spain.png"))); countries[7] = new Country("United States", imgUnitedStates = new BitmapImage(new Uri("pack://application:,,,/Images/unitedstates.png"))); cmbCountry.ItemsSource = countries; cmbCountry.SelectedValuePath = "Name"; cmbCountry.DisplayMemberPath = "Name"; cmbCountry.UpdateLayout(); imgFlag.Source = countries[counter].FlagImage; } private void btnNext_Click(object sender, RoutedEventArgs e) { if (counter ==0) { counter++; } else if (counter < 8) { imgFlag.Source = countries[counter].FlagImage; counter++; } } private void btnSubmit_Click(object sender, RoutedEventArgs e) { if (((Country)(cmbCountry.SelectedItem)).Name.Equals(imgFlag.)) //Here is were I need to cast it { txtAnswer.Text = "Correct"; } } } class Country { private string _name; private BitmapImage _flagImage; public Country(string name, BitmapImage image ) { Name = name; FlagImage = image; } public string Name { get { return _name; } set { try { _name = value; } catch (Exception ex) { throw new Exception(ex.Message); } } } public BitmapImage FlagImage { get { return _flagImage; } set { try { _flagImage = value; } catch (Exception ex) { throw new Exception(ex.Message); } } } public override string ToString() { return Name; } }