2012-03-19 6 views
0

からプロパティを取得する:私はこのようなビューモデルを得たViewModelに

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); 
     } 
    } 
} 

私はxaml.csにパラメータとして送信

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

を、私はそれを使用してXAML throughtアクセスするために何を書きますかバインディング??私は複数の例を理解できませんでした。

+0

'OnPropertyChanged'が後**と呼ばれるべきです**値を変更する。 –

+0

@ H.B。 yes = D fixed – Nahum

+0

[データバインディングの概要](http://msdn.microsoft.com/en-us/library/ms752347.aspx)をお読みください。 –

答えて

2
少しビューの背後にあるコードを変更

public partial class BaseView : Window 
{ 
    BaseViewModelTech viewModel; 

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

をしてからXAMLであなたはこのような何か持つことができます。

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

はまた、あなたのセッターにあなたがやりたいことに注意してください通知の後ではなく、プロパティ値が変更されています。

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

はい!ありがとう!私は数ヶ月間WPFを締め付けずに突然昨日の締め切りのプロジェクトを得ました= D – Nahum

1

あなたのビューのメンバであるVMインスタンスがあるため、直接ViewModelをxamlに直接参照することはできません。だから、あなたが最初のコードビハインドでビューのDataContextのを設定する必要があります。

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

を、ラベルのために例えば、あなたの私xaml.xamlに:

<Label Content="{Binding TechnicianID }"/> 
関連する問題