View要素のMVVM更新
-
20-12-2019 - |
質問
MVVM方式に続いてアプリがあります。私は複数の視野と視覚的なモデルを持っています。 私のMainpageでは、選択した要素からの情報を更新したいというテキストブロックがあります。
アプリを起動すると、MainViewModelから値を挿入してバインディングをテストするので、すべてがここで動作します。コードが次のとおりです。
<TextBlock Text="{Binding colorOfElement}" Grid.Row="1"/>
.
MainViewModel
のコード private string _colorOfElement;
public string colorOfElement
{
get
{
return _colorOfElement;
}
set
{
_colorOfElement = value;
NotifyPropertyChanged("colorOfElement");
}
}
.
......
colorOfElement = "Test";
.
正しく表示されます。ユーザが新しいViewModel内でイベントがイベントを発生させる要素と対話すると、ここではMainViewModelへの参照がありますので、String ColorOfElementを簡単に更新できます。
private MainViewModel mv;
......
public void MouseDown(ManipulationStartedEventArgs obj)
{
FrameworkElement MovingElement = (FrameworkElement)obj.OriginalSource;
Canvas canvas = FindParentOfType<Canvas>(MovingGear);
obj.ManipulationContainer = canvas;
obj.Handled = true;
testViewModel viewModel = (testViewModel)MovingElement.DataContext;
mv.colorOfElement = viewModel.model.Color;
}
.
この関数が実行されると、MainViewModelに送信され、NotifyPropertyChangedが起動されます。しかし、アプリケーションでは、ビューを表示するPAGEでは、変数の更新が表示されますが、変数が変化するコードにはありません。この拘束力のある問題についての考えは?
回答 私がDataContextをテストしたので、問題が問題を取り組んでいたので、私のパブリッククラスにありました。
public class MainViewModel : ViewModelBase
.
ここでは、機能を有効にするためのInotifyPropertyChangedインタフェースを含める必要があります。そのため、簡単な解決策はこれを追加してGET:
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
.
それからあなたは行くのが良いです:)
解決
それを確認してください:
- DataContextはmainviewmodel の正しいインスタンスに設定されています
- MainViewModelはInotifyPropertyChangedインターフェース を実装しています
所属していません StackOverflow