从用户控件更改 Windows 内容控件 - WPF
Change Windows Content Control From User Control - WPF
在我的项目中,我有一个名为 AccountWindow.xaml 的 Window,它有一个 ContentControl 来显示两个 UserControl。
帐号Window
<Window>
<Window.Resources>
<!-- Login User Control Template -->
<DataTemplate x:Name="LoginUserControl" DataType="{x:Type ViewModels:LoginViewModel}">
<AccountViews:LoginUserControl DataContext="{Binding}"/>
</DataTemplate>
<!-- Registration User Control Template -->
<DataTemplate x:Name="RegistrationUserControl" DataType="{x:Type ViewModels:RegistrationViewModel}">
<AccountViews:RegistrationUserControl DataContext="{Binding}" />
</DataTemplate>
</Window.Resources>
<Grid>
<!-- ContentControl that displays the two User Controls -->
<ContentControl Content="{Binding}" />
</Grid>
</Window>
然后我有两个用户控件,分别称为 LoginUserControl 和 RegistrationUserControl
登录用户控制
<Grid Background="Pink">
<Button Content="Switch To Register View" Command="{Binding SwitchToReg}" Margin="100" />
</Grid>
注册用户控件
<Grid Background="Orange">
<Button Content="Press Me" Command="{Binding PressMe}" Margin="100" />
</Grid>
登录用户控件和注册用户控件都有自己的 ViewModel,其中包含绑定到按钮的 RelayCommand,如代码所示。
登录视图模型
public class LoginViewModel
{
public RelayCommand SwitchToReg
{
get
{
return new RelayCommand(param =>
{
Console.WriteLine("Switch To Reg");
// Somehow change the content control in the AccountWindow to show the RegistrationDataTemplate???
});
}
}
}
问题
我希望能够在用户按下 UserControls 中的按钮之一时更改 AccountWindow 中 ContentControl 的内容。例如,当用户按下登录用户控件中名为 "Switch To Register View" 的按钮时,它会执行命令 SwitchToReg 并将内容控件更改为 RegistrationUserControl 及其 ViewModel。这怎么可能?
您可以创建一个 属性 并将其附加到控件。
或者您可以创建另一个用户控件并使其可见或不受您创建的 属性 控制。
为此,您需要在构建 UserControl 时将 AccountWindow 的引用传递给 UserControl,然后您的 Command 可以使用您提供的引用更新 ContentControl。
这引入了最好避免的耦合,因此我建议考虑 AccountWindow 的设计。我会使用网格行将 ContentControl 区域与将更改 UserControl 的按钮分开。
在上面的 window 中,蓝色区域是我放置 ContentControl 的地方,红色区域是 AccountWindow 的一部分。
这样,切换 ContentControl 的行为完全由 AccountWindow 处理。
在我的项目中,我有一个名为 AccountWindow.xaml 的 Window,它有一个 ContentControl 来显示两个 UserControl。
帐号Window
<Window>
<Window.Resources>
<!-- Login User Control Template -->
<DataTemplate x:Name="LoginUserControl" DataType="{x:Type ViewModels:LoginViewModel}">
<AccountViews:LoginUserControl DataContext="{Binding}"/>
</DataTemplate>
<!-- Registration User Control Template -->
<DataTemplate x:Name="RegistrationUserControl" DataType="{x:Type ViewModels:RegistrationViewModel}">
<AccountViews:RegistrationUserControl DataContext="{Binding}" />
</DataTemplate>
</Window.Resources>
<Grid>
<!-- ContentControl that displays the two User Controls -->
<ContentControl Content="{Binding}" />
</Grid>
</Window>
然后我有两个用户控件,分别称为 LoginUserControl 和 RegistrationUserControl
登录用户控制
<Grid Background="Pink">
<Button Content="Switch To Register View" Command="{Binding SwitchToReg}" Margin="100" />
</Grid>
注册用户控件
<Grid Background="Orange">
<Button Content="Press Me" Command="{Binding PressMe}" Margin="100" />
</Grid>
登录用户控件和注册用户控件都有自己的 ViewModel,其中包含绑定到按钮的 RelayCommand,如代码所示。
登录视图模型
public class LoginViewModel
{
public RelayCommand SwitchToReg
{
get
{
return new RelayCommand(param =>
{
Console.WriteLine("Switch To Reg");
// Somehow change the content control in the AccountWindow to show the RegistrationDataTemplate???
});
}
}
}
问题
我希望能够在用户按下 UserControls 中的按钮之一时更改 AccountWindow 中 ContentControl 的内容。例如,当用户按下登录用户控件中名为 "Switch To Register View" 的按钮时,它会执行命令 SwitchToReg 并将内容控件更改为 RegistrationUserControl 及其 ViewModel。这怎么可能?
您可以创建一个 属性 并将其附加到控件。 或者您可以创建另一个用户控件并使其可见或不受您创建的 属性 控制。
为此,您需要在构建 UserControl 时将 AccountWindow 的引用传递给 UserControl,然后您的 Command 可以使用您提供的引用更新 ContentControl。
这引入了最好避免的耦合,因此我建议考虑 AccountWindow 的设计。我会使用网格行将 ContentControl 区域与将更改 UserControl 的按钮分开。
在上面的 window 中,蓝色区域是我放置 ContentControl 的地方,红色区域是 AccountWindow 的一部分。
这样,切换 ContentControl 的行为完全由 AccountWindow 处理。