.net 如何使用局部变量使用两个UserControl之一填充主窗口
我对WPF或C并不陌生,但我对MVVM和ViewModel的情况还不熟悉 我一直在学习MVVM和绑定的基础知识,以了解我需要完成的工作,但我仍然没有完成 被xaml的细节绊倒了 我的最终目标是创建一个空白的MainWindow.xaml视图,该视图由两个UserControl.xaml视图中的一个填充 我有两个简单的用户控制视图:MainView.xaml和OptionalView.xaml.net 如何使用局部变量使用两个UserControl之一填充主窗口,.net,wpf,mvvm,.net,Wpf,Mvvm,我对WPF或C并不陌生,但我对MVVM和ViewModel的情况还不熟悉 我一直在学习MVVM和绑定的基础知识,以了解我需要完成的工作,但我仍然没有完成 被xaml的细节绊倒了 我的最终目标是创建一个空白的MainWindow.xaml视图,该视图由两个UserControl.xaml视图中的一个填充 我有两个简单的用户控制视图:MainView.xaml和OptionalView.xaml <UserControl x:Class="TestViewSwap.MainView"
<UserControl x:Class="TestViewSwap.MainView"
...>
<Grid>
<TextBlock Text="Main View" />
</Grid>
和MainWindow.xaml
<Window x:Class="TestViewSwap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<ContentControl Content="{Binding CurrentView}" />
</Window>
我知道CurrentView需要在代码隐藏中设置为OptionalView或MainView
依赖于我所选择的,但我不知道的是。。。什么类型的CurrentView
需要是或需要在什么代码隐藏文件中?CurrentView应该是MainWindowViewModel或Window.xaml上的Datacontext类的属性。为每个视图定义一个数据模板。数据模板可以包含指向用户控件的视图或点。将CurrentView指定给视图模型以切换视图。下面是一些代码: MainWindowViewModel
public class MainWindowViewModel : ViewModel
{
object currentView;
public MainWindowViewModel()
{
CurrentView = new OptionalView();
SwitchViewCommand = new RelayCommand(SwitchView);
}
public object CurrentView
{
get { return this.currentView; }
set
{
this.currentView = value;
NotifyPropertyChanged("CurrentView");
}
}
public RelayCommand SwitchViewCommand { get; set; }
void SwitchView()
{
if (CurrentView is OptionalView)
CurrentView = new SettingsView();
else
CurrentView = new OptionalView();
}
}
public class OptionalView { }
public partial class SettingsView { }
主窗口
<Window x:Class="WpfLab.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:WpfLab.Views"
xmlns:vm="clr-namespace:WpfLab.ViewModels"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:OptionalView}">
<Border Background="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingsView}">
<views:SettingsView />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding CurrentView}" />
<Button Grid.Row="1"
Command="{Binding SwitchViewCommand}"
Content="SwitchView" />
</Grid>
<UserControl x:Class="WpfLab.Views.SettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Border Background="Green" />
设置视图
<Window x:Class="WpfLab.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:WpfLab.Views"
xmlns:vm="clr-namespace:WpfLab.ViewModels"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:OptionalView}">
<Border Background="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingsView}">
<views:SettingsView />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding CurrentView}" />
<Button Grid.Row="1"
Command="{Binding SwitchViewCommand}"
Content="SwitchView" />
</Grid>
<UserControl x:Class="WpfLab.Views.SettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Border Background="Green" />
CurrentView应该是MainWindowViewModel或Window.xaml上的Datacontext类的属性。为每个视图定义一个数据模板。数据模板可以包含指向用户控件的视图或点。将CurrentView指定给视图模型以切换视图。下面是一些代码: MainWindowViewModel
public class MainWindowViewModel : ViewModel
{
object currentView;
public MainWindowViewModel()
{
CurrentView = new OptionalView();
SwitchViewCommand = new RelayCommand(SwitchView);
}
public object CurrentView
{
get { return this.currentView; }
set
{
this.currentView = value;
NotifyPropertyChanged("CurrentView");
}
}
public RelayCommand SwitchViewCommand { get; set; }
void SwitchView()
{
if (CurrentView is OptionalView)
CurrentView = new SettingsView();
else
CurrentView = new OptionalView();
}
}
public class OptionalView { }
public partial class SettingsView { }
主窗口
<Window x:Class="WpfLab.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:WpfLab.Views"
xmlns:vm="clr-namespace:WpfLab.ViewModels"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:OptionalView}">
<Border Background="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingsView}">
<views:SettingsView />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding CurrentView}" />
<Button Grid.Row="1"
Command="{Binding SwitchViewCommand}"
Content="SwitchView" />
</Grid>
<UserControl x:Class="WpfLab.Views.SettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Border Background="Green" />
设置视图
<Window x:Class="WpfLab.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:WpfLab.Views"
xmlns:vm="clr-namespace:WpfLab.ViewModels"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:OptionalView}">
<Border Background="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingsView}">
<views:SettingsView />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding CurrentView}" />
<Button Grid.Row="1"
Command="{Binding SwitchViewCommand}"
Content="SwitchView" />
</Grid>
<UserControl x:Class="WpfLab.Views.SettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Border Background="Green" />
Sleff-我正试图解决完全相同的问题,并有了一些开端。你能在我的电子邮件地址给我打电话吗?我们可以合作、发帖和回复,为其他人的利益服务。我正试图解决完全相同的问题,并有一些开端。你能在我的电子邮件地址给我打电话吗?我们可以合作、发帖和回复,以利于其他人?理解你的建议-将CurrentView作为公共属性显然是必要的-但是如何通过该属性绑定用户控件(这意味着如果MainWindowViewModel中的属性设置为value=OptionalView)-那么如何在Mainwindow.xaml中转换该属性并呈现可选的视图用户控件。这里假设usercontrols没有任何ViewModels,并且是简单的usercontrols(只有普通的xaml代码)我已经更新了我的答案。即使在每个视图前不需要单独的视图模型,也可以轻松地呈现不同的视图。谢谢,我在单独的视图模型之间摇摆不定对于每个视图和非视图,因此我目前没有偏好,也没有从应用程序需求的角度对其进行特定的需求。我现在来看看这对我有什么作用。我已经看到了两个示例-一个是每个用户控件都有一个viewmodel(因此可以引用)还有一个没有。下面是示例:和Per-理解您的建议-将CurrentView作为公共属性显然是必要的-但是如何通过该属性绑定任何一个usercontrol(意味着如果MainWindowViewModel中的属性设置为value=OptionalView)-那么如何在Mainwindow.xaml中转换它并呈现可选的视图用户控件。这里假设usercontrols没有任何ViewModels,是简单的usercontrols(只有普通的xaml代码)我已经更新了我的答案。即使在每个视图前不需要单独的视图模型,也可以轻松地呈现不同的视图。谢谢,我在单独的视图模型之间摇摆不定对于每个视图和非视图,我目前没有偏好,也没有从应用程序需求的角度对其进行特定的需求。我现在来看看这对我有什么作用。我已经看到了两个示例-一个是每个用户控件都有一个viewmodel(因此可以引用),另一个是没有。下面是示例:和