10 Replies - 6036 Views - Last Post: 27 January 2013 - 11:57 AM Rate Topic: -----

#1 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

Getting the object from a wpf Image control

Posted 27 January 2013 - 09:59 AM

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.

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



Is This A Good Question/Topic? 0
  • +

Replies To: Getting the object from a wpf Image control

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 10:21 AM

Quote

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.


Something is really screwed up here.

Quote

countries[0] = new Country("Australia", imgAustralia = new BitmapImage(new Uri("pack://application:,,,/Images/australia.png")));


Your country class doesn't have a property of imgAustralia, imgBrazil and so on. And a bunch of commas in the URI?
Does this code even compile and run? Because I would expect all sorts of errors from this.

If the URI's wered fixed I might expect this to work where you aren't trying to make up variable names like imgAustralia =:
countries[0] = new Country("Australia", new BitmapImage(new Uri("pack://application:../../Images/australia.png")));

Was This Post Helpful? 0
  • +
  • -

#3 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 10:24 AM

Yes it complies and runs perfectly.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 10:25 AM

How about posting the XAML for the window?
Was This Post Helpful? 0
  • +
  • -

#5 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 10:27 AM

sure sorry.
<Window x:Class="_Exercise_10.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF Images Exercise" Height="212.687" Width="525">
    <Grid Margin="0,0,0,-3" Background="Transparent">
        <GroupBox Header="Flag" HorizontalAlignment="Left" Margin="23,37,0,0" VerticalAlignment="Top" Width="135" Height="108">
            <Image x:Name="imgFlag" HorizontalAlignment="Left" Height="84" VerticalAlignment="Top" Width="123"/>
        </GroupBox>
        <GroupBox Header="Select Country" HorizontalAlignment="Left" Margin="163,37,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.543,-1.084" Width="229" Height="108"/>
        <ComboBox x:Name="cmbCountry" HorizontalAlignment="Left" Margin="196,66,0,0" VerticalAlignment="Top" Width="180" />
        <TextBox x:Name="txtAnswer" HorizontalAlignment="Left" Height="23" Margin="196,110,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="180" RenderTransformOrigin="0.151,-3.879"/>
        <Button x:Name="btnSubmit" Content="Submit" HorizontalAlignment="Left" Margin="418,51,0,0" VerticalAlignment="Top" Width="75" Click="btnSubmit_Click"/>
        <Button x:Name="btnNext" Content="Next Flag" HorizontalAlignment="Left" Margin="418,88,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.136,-0.059" Click="btnNext_Click"/>
        <Button x:Name="btnScore" Content="0/0" HorizontalAlignment="Left" Margin="418,125,0,0" VerticalAlignment="Top" Width="75" IsEnabled="False" />
    </Grid>
</Window>


Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 10:32 AM

Oh- I see why it is working. Its oddly done, but it works.

Lines 4-10: If you aren't using these bitmap references elsewhere then don't keep them around to eat memory. You can just do as I showed
countries[0] = new Country("Australia", new BitmapImage(new Uri("pack://application:,,,Images/australia.png")));


If you are using these throughout the program, define them up front then use the references in your links.

private BitmapImage imgAustralia = new BitmapImage(new Uri("pack://application:,,,/Images/australia.png");

countries[0] = new Country("Australia", imgAustralia );



Isn't that a lot cleaner?
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 10:39 AM

Quote

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.


That makes my brain hurt. What are you trying to do?
If the user selects "USA" from the combobox you want to find the Country in the Countries[] whose name is "USA"?
First: Don't make people do two things when one will do
Instead of making a selection and having to click a button, wouldn't it be nice if just making the selection would do? If you search for your country on the SelectedIndexChange event of the combobox your users wouldn't have to hit a submit button
 <ComboBox x:Name="cmbCountry" HorizontalAlignment="Left" Margin="196,66,0,0" VerticalAlignment="Top" Width="180" />


Second: Finding the country by name is just a matter of looping through the array and doing an if comparison.

psuedo code (since this is homework we can't give you working code)
loop
    if countries[index].name.tolower == selecedname.tolower - do something
    break out of loop since we have a match
endloop

Was This Post Helpful? 0
  • +
  • -

#8 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 10:40 AM

Yes, thank you This is not my design or idea it is a home work assignment. I think it is awful also. I still don't understand what the textbox is for the
user is suppose to cycle through the flags and select the correct country name from the combobox that belongs to that flag.Thank you for your help.

This post has been edited by aortell24: 27 January 2013 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 11:00 AM

You're not going to cast an image to a country.

But if you have looped through all the elements of your country array until you found the matching name... You have just found the country object you needed.

By the same token, images are reference types. So if you are displaying an image you can loop through the country array doing an if comparison to see if the displayed flag is the same image as in one of the country instances. Then get the name.


Quote

I still don't understand what the textbox is for

Stop. If you don't understand the assignment yourself then you need to talk to the teacher. You can't code what you don't comprehend. Its like being told 'I want you to drive to alkdfjklhgksjhfgkjsdfh'. And you say 'okay'. But you don't know where 'alkdfjklhgksjhfgkjsdfh' is. How are you going to plan a route to get there?
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 11:34 AM

Ok - after completely building a working version of this I see what it is. Its a quiz program to identify the flag.

A flag image is displayed.
The user then picks what country they think the flags belongs to, from the combobox.
When the user hits [submit] they are told if they are right or wrong.
I would guess that the textbox is supposed to show the correct country.

Posted Image

Compare the combobox selected item text to the currently selected countries array object name.

Your counter variable is telling you which country is currently on display. Take its name and see if its the same as in the combobox.

REMINDER TO OTHERS:
This is homework. Do not supply code.

This post has been edited by tlhIn`toq: 27 January 2013 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: Getting the object from a wpf Image control

Posted 27 January 2013 - 11:57 AM

View Postaortell24, on 27 January 2013 - 11:40 AM, said:

I still don't understand what the textbox is for


Look at your code:

Quote

049               txtAnswer.Text = "Correct";



Doesn't that tell you what the textbox is for?

I think you really need to just pay more attention and ask questions while in class. Actually understanding the assignment and the lecture would probably do you wonders.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1