我正在开发一个使用 MVVM 和 WPF 的项目,但我遇到了困难。
当我创建一个Button
and a ContentControl
在按钮更改内容的窗口中ContentControl
,效果很好。
<Window.Resources>
<me:UserControl1ViewModel x:Key="viewModel" />
</Window.Resources>
<Grid>
<Button Content="Button"
Name="button1"
Command="{Binding Source={StaticResource viewModel}, Path=ClickCommand}" />
<ContentControl Content="{Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Source={StaticResource viewModel}, Path=View, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />
</Grid>
但是,当我创建带有按钮的 UserControl 并且按钮更改 ContentControl 的内容时不起作用。
为什么?
<Window.Resources>
<me:UserControl1ViewModel x:Key="viewModel" />
</Window.Resources>
<Grid>
<v:UserControl1 />
<ContentControl Content="{Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Source={StaticResource viewModel}, Path=View, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />
</Grid>
正在调用 ContentControl 内容更改的 UserControl
<UserControl.Resources>
<me:UserControl1ViewModel x:Key="viewModelA" />
</UserControl.Resources>
<Grid>
<Button Content="Button"
Name="button1"
Command="{Binding Source={StaticResource viewModelA}, Path=ClickCommand}" />
</Grid>
Thanks!