views:

101

answers:

1

In My ViewModel class I have a property:

class ViewModel : INotifyPropertyChanged
{
public string FileName {get;set;}
}

and in my View I bind a label's content to ViewModel's FileName.

now When I do drag-drop a file to my View, How can I update the label's Content property, so that the ViewMode's FileName also get updated via binding?

Directly set the label's Content property won't work, it just simply clear the binding.

A: 

If you have to do it in code-behind, you can do something like that :

ViewModel vm = (ViewModel)this.DataContext;
vm.FileName = path;

(btw, your ViewModel class needs to implement INotifyPropertyChanged)

Thomas Levesque
then the View will have to know about the ViewModel, is this anti-pattern?
Benny
I did implement INotifyPropertyChanged
Benny
Well, the view always knows about the ViewModel, since it has a reference to it (DataContext) and has bindings to its properties... If the ViewModel knew about the view, *then* you would be breaking the pattern
Thomas Levesque
@Thomas, code is ugly :), but acceptable.
Benny