dimanche 24 mai 2015

wpf checbox and image binding

I have one image that changes depending on the status of 4 checboxes. I made the code work but it seems too long and complicated, I am new to wpf and can't make binding to work with these 4 checkboxes. Any source for help or example you can give will be great. This is what I have right now:

    private void chcbox1_Click(object sender, RoutedEventArgs e)
    {
        if (chcbox1.IsChecked == true)
        {
            chcbox2.IsChecked = false;
            var02 = true;
            var01 = false;
            if (var03)
                img01.Source = new BitmapImage(new Uri("Images/Image04.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image03.png", UriKind.Relative));
        }
        else
        { 
            chcbox2.IsChecked = true;
            var02 = false;
            var01 = true;
            if (var03)
                img01.Source = new BitmapImage(new Uri("Images/Image02.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image01.png", UriKind.Relative));
        }                
    }

    private void chcbox2_Click(object sender, RoutedEventArgs e)
    {
        if (chcbox2.IsChecked == true)
        {
            chcbox1.IsChecked = false;
            var02 = false;
            var01 = true;
            if (var03)
                img01.Source = new BitmapImage(new Uri("Images/Image02.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image01.png", UriKind.Relative));
        }
        else
        {
            chcbox1.IsChecked = true;
            var02 = true;
            var01 = false;
            if (var03)
                img01.Source = new BitmapImage(new Uri("Images/Image04.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image03.png", UriKind.Relative));
        }
    }

    private void chcbox3_Click(object sender, RoutedEventArgs e)
    {
        if (chcbox3.IsChecked == true)
        {
            chcbox4.IsChecked = false;
            var04 = true;
            var03 = false;
            if (var01)
                img01.Source = new BitmapImage(new Uri("Images/Image01.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image03.png", UriKind.Relative));                
        }
        else
        {
            chcbox4.IsChecked = true;
            var04 = false;
            var03 = true;
            if (var01)
                img01.Source = new BitmapImage(new Uri("Images/Image02.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image04.png", UriKind.Relative));   
        }
    }

    private void chcbox4_Click(object sender, RoutedEventArgs e)
    {
        if (chcbox4.IsChecked == true)
        {
            chcbox3.IsChecked = false;
            var04 = false;
            var03 = true;
            if (var01)
                img01.Source = new BitmapImage(new Uri("Images/Image02.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image04.png", UriKind.Relative)); 
        }
        else
        {
            chcbox3.IsChecked = true;
            var04 = true;
            var03 = false;
            if (var01)
                img01.Source = new BitmapImage(new Uri("Images/Image01.png", UriKind.Relative));
            else
                img01.Source = new BitmapImage(new Uri("Images/Image03.png", UriKind.Relative));  
        }
    }

checkbox1 and checkbox2 are mutually exclusive, as are checkbox3 and checkbox4 So from all the 4 combinations I get 4 different images.

Thanks for your help. Eduardo




Aucun commentaire:

Enregistrer un commentaire