Forums

Change image based on data event
Last Post 03 Jun 2016 08:54 PM by J. Alan Atherton. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
J. Alan AthertonUser is Offline
New Member
New Member
Posts:45


--
02 Jun 2016 04:34 PM
    I have an image that I would like to change based on a variable. It's a battery charge level icon.
    At the moment I am doing this in code like this:
            public void SetBatteryLevel(int level)
            {
                if(level == 0)
                   batteryIcon.Source = (BitmapImage)FindResource("batteryIcon_0");
                else if(level == 1)
                    batteryIcon.Source = (BitmapImage)FindResource("batteryIcon_25");
                else if (level == 2)
                    batteryIcon.Source = (BitmapImage)FindResource("batteryIcon_50");
                else if (level == 3)
                    batteryIcon.Source = (BitmapImage)FindResource("batteryIcon_75");
                else if (level == 4)
                    batteryIcon.Source = (BitmapImage)FindResource("batteryIcon_100");
    
                if (level < 2)
                    batteryIcon.Visibility = Visibility.Visible;
    
                else
                    batteryIcon.Visibility = Visibility.Hidden;
            }
    


    I would like to know if there is any way to do this with XAML and MVVM. I see WPF examples with DataTriggers and such and can see some possibilities, but I don't know what EK supports or how to go about this.

    Is the way I am doing it the best way for EK?
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:676


    --
    03 Jun 2016 09:04 AM
    I have property of type BitmapImage in view model and binding in Image Source. All the UI logic is in view model. That's what I do.
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    03 Jun 2016 08:54 PM
    Ah, that makes a lot more sense. I don't know why I didn't think of it
    You are not authorized to post a reply.