UserControl需要设置静态资源,Window同理
1,添加资源命名空间
xmlns:viewModel="clr-namespace:项目名.ViewModel"
2,设置静态资源
UserControl添加静态资源
<UserControl.Resources>
<viewModel:SwitchViewModel x:Key="switchViewMode"/>
<viewModel:SwitchViewModel1 x:Key="switchViewMode1"/>
</UserControl.Resources>
3,DataContext 绑定静态资源
<usercontrol:TaskList DataContext="{StaticResource switchViewMode}"/></Grid>
经过这3步就可以实现相同Xaml绑定不同DataContext的操作了。

本文介绍如何在WPF应用程序中为UserControl设置静态资源,并通过绑定不同的DataContext实现Xaml复用。主要步骤包括:添加资源命名空间、设置静态资源及DataContext绑定。
1338

被折叠的 条评论
为什么被折叠?



