Nahum
Nahum

Reputation: 7197

getting the property from ViewModel

I got a view model like this:

 public class BaseViewModelTech : INotifyPropertyChanged
{


    static string _TechnicianID;
    public string TechnicianID
    {
        get {                
            return _TechnicianID;
        }
        set {

            _TechnicianID = TechnicianID; 
            OnPropertyChanged("TechnicianID");
        }

    }

    static string _DeviceID;
    public string DeviceID
    {
        get
        {
            return _DeviceID;
        }
        set
        {
            _DeviceID = DeviceID;
            OnPropertyChanged("DeviceID");
        }

    }



    // In ViewModelBase.cs
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.VerifyPropertyName(propertyName);

        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    public void VerifyPropertyName(string propertyName)
    {
        // Verify that the property name matches a real,  
        // public, instance property on this object.
        if (TypeDescriptor.GetProperties(this)[propertyName] == null)
        {
            string msg = "Invalid property name: " + propertyName;
            Debug.Fail(msg);
        }
    }
}

I send it as parameter to my xaml.cs

 public partial class BaseView   : Window{
  BaseViewModelTech  viewModel; 
        public BaseView  (BaseViewModelTech   vm)
        {
            InitializeComponent();
            viewModel = vm;
        }}

what do I write to access it throught xaml using binding?? I failed to understand multiple examples.

Upvotes: 0

Views: 684

Answers (2)

Talal
Talal

Reputation: 119

In your case you can't directly refer your ViewModel directly into xaml due to you vm instance being member of your View. So, you should set the DataContext of your view first in code-behind:

  public partial class BaseView   : Window{
  BaseViewModelTech  viewModel; 
        public BaseView  (BaseViewModelTech   vm)
        {
            InitializeComponent();
            viewModel = vm;
            this.DataContext=viewModel;
        }}

then in your my xaml.xaml for example for label :

    <Label Content="{Binding TechnicianID }"/>

Upvotes: 1

slugster
slugster

Reputation: 49965

Change your code behind of your view slightly:

public partial class BaseView   : Window
{
    BaseViewModelTech  viewModel; 

    public BaseView  (BaseViewModelTech   vm)
    {
        InitializeComponent();
        viewModel = vm;
        this.DataContext = vm;   // <----------- add this
    }
}

And then in your XAML you can have something like this:

<TextBlock Text="{Binding TechnicianID}" />

Also note that in your setters you want to do the notification after the property value is changed, not before:

    set
    {
        _DeviceID = DeviceID;
        OnPropertyChanged("DeviceID");  // <------ this goes after the member variable change
    }

Upvotes: 2

Related Questions