2011-07-28 7 views
0

確かにこの質問は数千回議論されていましたが、私のニーズに適した解決策を見つけることはできません。私はSilverLIghtが初めてで、MVVMを使い始めようとしました。viewModelのプロパティを介したデータバインド

public class MyViewModel 
    { 
      private IRepository _Repository; 
      public string CountText { get; set; } 
      public MyViewModel (IRepository repository) 
     { 

      _Repository = repository; 
      CountText = "test ctor"; 
     } 

     public void MyButtonCommand() 
     { 
      _Repository.GetResult((Result r) => MyActionAsync(r), (Exception e) => ManageException(e)); 
     } 

public void MyActionAsync(SchedeConsunitiviResult result) 
     { 
      CountText = string.Format("{0} items", result.Count); 
     } 

     public void ManageException(Exception e) 
     { 
      //to log the exception here and display some alert message 
     } 

} 

とここに私のXAML: したがって、私は、次のビューモデルでした

<sdk:Label Content="{Binding Path=CountText, Mode=TwoWay}" Grid.Row="3" Height="28" HorizontalAlignment="Left" Margin="12,142,0,0" Name="label1" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" /> 

をCountTextの最初のインスタンス化は、ラベルに表示されます。しかし、非同期メソッドの後の2番目のメソッドは、LAbelの内容を変更しません。このプロパティが変更されたビューを伝えるために、PropertyChangedのようなメカニズムを追加する必要がありますか?もしそうなら、私はxamlだけを使ってそれをどうやって行うことができますか?あなたの助けのための

THX

答えて

2

INotifyPropertyChangedを実装し、あなたの財産は、イベントハンドラで変更されたことを通知します。

public class MyViewModel : INotifyPropertyChanged 
{ 
    private string countText; 

    public string CountText   
    { 
     get { return this.countText; } 
     set { this.countText = value; NotifyPropertyChanged("CountText"); } 
    } 

    .....snip..... 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(params string[] properties) 
    { 
     if (PropertyChanged != null) 
     { 
      foreach (string property in properties) 
       PropertyChanged.Invoke(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 
+1

YouhouでのPropertyChangedのような仕組みが必要です知っているように、それは魔法のように動作します! – Arthis

0

は、私の知る限り、あなたのviewmodel

+0

答えのためのthx私はArcturusのコードをうまく使用しましたが、あなたも正しいです! – Arthis

関連する問題