對於這一切,你會使用綁定作爲方法「通信」。例如,確認消息可能會根據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