Home > front end >  Change image source when property value changed
Change image source when property value changed

Time:04-20

I have this textblock and image icon in HomePage.xaml,

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        x:Class="MyApp.HomePage">
        d:DataContext="{d:DesignInstance Type=HomePageViewModel}">
<Grid>
     <TextBlock Text="{Binding Username}"></TextBlock>
     <Image Source="{Binding ImageSource}" Width="45"></Image>
</Grid>
</Window>

I have this HomePageViewModel class but these Username and ImageSource properties are in another one viewmodel named WebAppViewModel.

internal class WebAppViewModel
{
   private string userName;
   public string Username
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                OnPropertyChanged();
            }
        }
        
        private bool hasSignedIn;
        public bool HasSignedIn
        {
            get
            {
                return hasSignedIn;
            }
            set
            {
                hasSignedIn = value;
                OnPropertyChanged();
            }
        }

        public string ImageSource
        {
         get; set;
        }
}

Depends on HasSignedIn value, need to show the image (Grey icon image/ Green icon image)

This user data coming from another one service from which I am assigning the values for these properties.

How I can bind these properties in HomePage.xaml ? and when HasSignedIn value changes that needs to be notified, so that proper color image can be shown and if HasSignedIn is false then TextBlock should be invisible.

CodePudding user response:

I have used OnPropertyChanged() in the ImageSource set property.

  • Related