对于这一切,你会使用绑定作为方法“通信”。例如,确认消息可能会根据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
}
}
}
这不是一个完整的示例(唯一的选择是肯定的!:)),但希望这说明了你的视图和你的ViewModel几乎是一个实体,而不是两个互相打电话。
希望这会有所帮助。
是的,这是方向我期待着去。只需稍微正式化一下即可。谢谢 – TheZenker 2009-12-30 18:25:19
好的...祝你好运正规化。 – 2009-12-30 18:34:10