1

I have a question. How to bind variables dynamically from ViewModel to View? For now, it not even displayed. If i not using Command, it works great (but of course, i can bind image only once).

My View:

namespace somestuff.View
{

     public partial class WindowView : Window
     {
         public WindowView()
         {
            this.DataContext = new WindowViewModel();
            InitializeComponent();
        }
    }
}

my View.Xaml (shorten):

<Image Source="{Binding DisplayedImage}"/>
<Button Command="{Binding NewImageCommand}"/>

And my ViewModel:

public WindowViewModel()
{
    _canExecute = true;
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public string DisplayedImage //displaying image
{
    get { return filepath; }

    set { filepath = value; NotifyPropertyChanged(nameof(filepath)); }
}

public string filepath { get; set; } //var for binding


private bool _canExecute;
private ICommand _newImageCommand; //command for button
public ICommand NewImageCommand
{
    get
    {
        return _newImageCommand ?? (_newImageCommand = new Commands.CommandHandler(() => GetImage(), _canExecute));
    }
}

public void GetImage() { filepath = Pictures.GetNewImage(); } //command on button click

Can you tell me, why after triggering Command GetImage() on button click the image binded on Image not changed? If i move filepath = Pictures.GetNewImage(); from command (more clear, i not use command) all works great, but i cant re-invoke binding to my Image. Can you tell me, how to bind propertis dynamically into View from View model? When value of variable (in this case, filepath) change, i want to change View control too.

Thanks for any advices.

EDIT:

I have 6 Image Labels. I displaying images in it like that:

public BitmapImage DisplayedHighPerformanceImage
    {
        get { return kMMHP; }

        set { kMMHP = value; NotifyPropertyChanged(nameof(kMMHP)); }
    }

So i need filepath to init 6 diffrent bitmaps. Then i work on that bitmaps (for exampe, that kMMHP) So i want to display every new bitmap initialized from kMMHP image.

kMMHP = method1(); //displaying it
//other stuff do with diffrent bmps
kMMHP = method2(); //displaying it after second method with changed values
7
  • 1
    Just implement the DisplayedImage property correctly, with NotifyPropertyChanged(nameof(DisplayedImage)). Then call DisplayedImage = Pictures.GetNewImage(); Commented Sep 10, 2018 at 18:29
  • 1
    Besides that, you shouldn't use a property as the "backing field" of another property. Just declare private string filepath; Commented Sep 10, 2018 at 18:34
  • 1
    Thank you @Clemens for your time. The thing isnt that simply as u mentioned. From filepath i init bunch of Bitmaps and BitmapImages. Then, i work on on this bitmaps. At all i have 6 Image labels, 3 bitmaps and 3 bitmapimages variables. So i need that string (filepath) to init 6 diffrent bmp variables. Then, i displaying by binding that vars. So i cant use simple DisplayedImg = Pictures... because then i work on bitmaps that i initialized from filepath Commented Sep 10, 2018 at 18:34
  • 1
    I displaying bitmap like that: public BitmapImage DisplayedHighPerformanceImage { get { return kMMHP; } set { kMMHP = value; NotifyPropertyChanged(nameof(kMMHP)); } } Commented Sep 10, 2018 at 18:37
  • 1
    I make an edit, check it if you can please and tell me, is it now clear Commented Sep 10, 2018 at 18:40

1 Answer 1

1

NotifyPropertyChanged must be called with the name of the property, not the name of its backing field. And in order to fire the change notification event, you have to set the property, not the backing field:

public BitmapImage DisplayedHighPerformanceImage
{
    get { return kMMHP; }
    set { kMMHP = value; NotifyPropertyChanged(nameof(DisplayedHighPerformanceImage)); }
}

DisplayedHighPerformanceImage = method1();
Sign up to request clarification or add additional context in comments.

2 Comments

Works great, thanks! Could you tell me, if i can every kMMHP values change for DisplayedHighPerformanceImage? Is it works as: kMMHP == DisplayedHighPerformance...?
Not sure what you mean exactly, but of course the value of the property and its backing field are always the same (since there is get { return kMMHP; }). The backing field is however private and can't be accessed from outside the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.