これらすべてについて、「コミュニケーション」の方法としてバインディングを使用します。たとえば、確認メッセージは、ViewModel で設定されたプロパティに基づいて表示または非表示になる場合があります。
ビューはこちら
<Window.Resources>
<BoolToVisibilityConverter x:key="boolToVis" />
</Window.Resources>
<Grid>
<TextBox Text="{Binding Comment, Mode=TwoWay}" />
<TextBlock Visibility="{Binding IsCommentConfirmationShown,
Converter={StaticResource boolToVis}"
Text="Are you sure you want to cancel?" />
<Button Command="CancelCommand" Text="{Binding CancelButtonText}" />
</Grid>
そして、ここにあなたのViewModelがあります
// for some base ViewModel you've created that implements INotifyPropertyChanged
public MyViewModel : ViewModel
{
//All props trigger property changed notification
//I've ommited the code for doing so for brevity
public string Comment { ... }
public string CancelButtonText { ... }
public bool IsCommentConfirmationShown { ... }
public RelayCommand CancelCommand { ... }
public MyViewModel()
{
CancelButtonText = "Cancel";
IsCommentConfirmationShown = false;
CancelCommand = new RelayCommand(Cancel);
}
public void Cancel()
{
if(Comment != null && !IsCommentConfirmationShown)
{
IsCommentConfirmationShown = true;
CancelButtonText = "Yes";
}
else
{
//perform cancel
}
}
}
これは完全なサンプルではありません (唯一のオプションは yes! :) ) ですが、うまくいけば、これは View と ViewModel がほぼ 1 つのエンティティであり、互いに電話をかけている 2 つのエンティティではないことを示しています。
お役に立てれば。