gpt4 book ai didi

.net - 如何使用局部变量用两个 UserControl 之一填充 MainWindow

转载 作者:行者123 更新时间:2023-12-03 10:39:07 24 4
gpt4 key购买 nike

我对 WPF 或 C# 并不陌生,但我对 MVVM 和 ViewModel 情况并不陌生。

我一直在通过 MVVM 和绑定(bind)基础知识来完成我需要完成的工作,但我仍然
被 xaml 细节绊倒。

我的最终目标是创建一个空白 MainWindow.xaml View ,其中填充了两个 UserControl.xaml View 之一。

我有 2 个简单的 UserControl View :MainView.xaml 和 OptionalView.xaml。

<UserControl x:Class="TestViewSwap.MainView"
...>
<Grid>
<TextBlock Text="Main View" />
</Grid>
<UserControl x:Class="TestViewSwap.OptionalView"
...>
<Grid>
<TextBlock Text="Optional 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 的任何类。为每个 View 定义一个数据模板。数据模板可以包含 View 或指向用户控件。将 CurrentView 分配给 View 模型以切换 View 。这是一些代码:

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>


设置 View
<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" />

关于.net - 如何使用局部变量用两个 UserControl 之一填充 MainWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12572466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com