gpt4 book ai didi

c# - 如何在 MVVM 项目中的用户控件之间切换

转载 作者:行者123 更新时间:2023-12-03 10:16:08 25 4
gpt4 key购买 nike

编辑 :尽管@AbinMathew 已将此问题标记为可能与 this 重复,为该问题提供的解决方案并没有很好地解释如何中继命令逻辑。正如我的回答中提到的,我能够在 John Smith 的文章的帮助下解决这个问题。

我有这个测试项目,我正在运行它以了解 MVVM。我想要实现的目标:MainWindow 有一个后退按钮和一个 ContentControl。在 Window_loaded 上,我想在 ContentControl 中显示 MainGadget。当我在 MainGadget 中单击 MyBtn 时,我想在 ContentControl 中显示 MyGadget。

ViewModelBase 是 MainGadgetVM、MainWindowVM 和 MyGadgetVM 使用的一个类。它实现了 INotifyPropertyChanged 接口(interface)。 RelayCommand 实现了 ICommand 接口(interface),所以我想用它来执行 MyBtn_Click 并显示其他 UserControl。

目前,当我运行程序时,只显示“返回”按钮。我似乎无法弄清楚如何显示其他用户控件。任何帮助都感激不尽。

Solution Explorer

数据模板.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ExampleContentCtrl.VMs"
xmlns:view="clr-namespace:ExampleContentCtrl.Panels">
<DataTemplate DataType="{x:Type vm:MainGadgetVM}">
<view:MainGadget/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MyGadgetVM}">
<view:MyGadget/>
</DataTemplate>

</ResourceDictionary>

主窗口.xaml
<Window x:Class="ExampleContentCtrl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="375" Width="300" Loaded="Window_Loaded">
<Window.Resources>
<ResourceDictionary Source="DataTemplates.xaml"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="8*"/>
</Grid.RowDefinitions>
<Button x:Name="BckSpace" Content="Back" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="0"/>
<ContentControl Grid.Row="1"/>
</Grid>
</Window>

MainGadget.xaml
<UserControl x:Class="ExampleContentCtrl.Panels.MainGadget"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button x:Name="MyBtn" Content="My Gadget" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="1" Command="{Binding MyBtn_Click}"/>
</Grid>

</UserControl>

主窗口.xaml.cs
namespace ExampleContentCtrl
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Load MainGadgetVM via MainWindowVM.Initialize()
}

}
}

MainWindowVM.cs
namespace ExampleContentCtrl.VMs
{
public class MainWindowVM : ViewModelBase
{
private RelayCommand _ShowWorkSpace;
private static MainWindowVM _Instance;
public static MainWindowVM Instance { get { return _Instance; } }

public MainWindowVM()
{
MainWindowVM._Instance = this;
}

public RelayCommand ShowWorkSpace
{
get
{
if (_ShowWorkSpace == null)
_ShowWorkSpace = new RelayCommand(param => { });
return _ShowWorkSpace;
}
}

public void Initialize()
{
//this.ShowWorkSpace.Execute("ExampleContentCtrl.VMs.MainGadgetVM");
}
}
}

最佳答案

将绑定(bind)添加到您的内容控件并将绑定(bind)值更改为您要显示的 View 模型。

<Window x:Class="ExampleContentCtrl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="375" Width="300" Loaded="Window_Loaded">
<Window.Resources>
<ResourceDictionary Source="DataTemplates.xaml"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="8*"/>
</Grid.RowDefinitions>
<Button x:Name="BckSpace" Content="Back" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="0"/>
<ContentControl Grid.Row="1" Content={Binding Path=MyContent}/>
</Grid>
</Window>


public class MainWindowVM
{
//...

public MainViewModel
{
MyContent = new TheViewModelThatShouldBeShownAtStart();
}

public object MyContent
{
get; private set; // add Notification!
}


void FunctionCalledWhenButtonIsPressed()
{
if (...) // add your logic here
MyContent = new VM1();
else
MyContent = new VM2();
}

}

关于c# - 如何在 MVVM 项目中的用户控件之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230086/

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