C# WPF用户控件嵌套控件-从Usercontrol切换Usercontrol

C# WPF用户控件嵌套控件-从Usercontrol切换Usercontrol,c#,wpf,xaml,user-interface,user-controls,C#,Wpf,Xaml,User Interface,User Controls,我目前正在用VisualStudio中的WPF开发一个应用程序,作为MySQL数据库的前端,该数据库将在学校中使用,以使硬件等的组织更加简单。 我对C#和WPF完全陌生,因此现在遇到了一个我在过去几个小时内无法解决的问题。 UI由一个带有导航栏等的窗口和一个用于显示当前用户控件的大框架/网格组成 单击我的主窗口导航栏中的按钮会触发一个事件,该事件会切换UserControl,而不会出现任何问题,只需使用以下行: ContentFrame.Children.Clear(); //ContentF

我目前正在用VisualStudio中的WPF开发一个应用程序,作为MySQL数据库的前端,该数据库将在学校中使用,以使硬件等的组织更加简单。 我对C#和WPF完全陌生,因此现在遇到了一个我在过去几个小时内无法解决的问题。 UI由一个带有导航栏等的窗口和一个用于显示当前用户控件的大框架/网格组成

单击我的主窗口导航栏中的按钮会触发一个事件,该事件会切换UserControl,而不会出现任何问题,只需使用以下行:

ContentFrame.Children.Clear();  //ContentFrame is a simple Grid which I am using ot display the UserControls
ContentFrame.Children.Add(new UserControlDashboard());  //UserControlDashboard is the Class of one of my UserControls
我不知道这是否真的是实现这一点的最佳方式(因为它总是重新加载UserControl),但至少它很简单且有效

问题是,我只能通过Mainwindow类切换UserControls。但是我希望能够在其中一个UserControls中切换UserControl。(例如,我的一个UserControl显示一个dataGrid,其中包含我的一个db表中的所有数据。通过双击其中一行,我希望能够将该表中的当前UserControl切换到另一行。)

但我真的不知道我该怎么做。我做了一些研究,但只找到了由不同类的douzen和许多不同的eventhandler等组成的解决方案。不幸的是,我无法真正理解该实现是如何工作的。它也被限制为2个用户控件

我有没有办法在合理的时间内实现这一点?我已经读到,可能通过使用路由事件来实现?由于我是C#的新手,我对事件、调度员等完全陌生,因此很难处理所有基于事件的东西D


谢谢:)

一个简单的解决方案是使用数据绑定:

main window.xaml

<Window>
  <StackPanel>
    <SwitchingControl x:Name="BindingSourceControl" />
    <ContentControl x:Name="ContentFrame" 
                    Content="{Binding ElementName=BindingSourceControl, Path=SelectedControl}" />
  </StackPanel>
</Window>

切换控件.xaml.cs

partial class SwitchingControl : UserControl
{
  public static readonly DependencyProperty SelectedControlProperty = DependencyProperty.Register(
    "SelectedControl",
    typeof(Control),
    typeof(SwitchingControl),
    new PropertyMetadata(default(Control)));

  public Control SelectedControl
  {
    get => (Control) GetValue(SwitchingControl.SelectedControlProperty);
    set => SetValue(SwitchingControl.SelectedControlProperty, value);
  }

  // Dictionary to store reusable controls
  private Dictionary<string, Control> ControlMap { get; set; }

  public SwitchingControl()
  {
    this.ControlMap = new Dictionary<string, Control>() 
    { 
      { nameof(UserControlDashboard), new UserControlDashboard()) }
    };
  }

  // TODO::Invoke when a DataGrid row was double clicked
  private void OnNewControlSelected(string selectedControlKey)
  {
    if (this.ControlMap.TryGetValue(selectedControlKey, out Control selectedControl)
    {
      this.SelectedControl = selectedControl;
    }
  }
}
部分类切换控件:UserControl
{
公共静态只读DependencyProperty SelectedControlProperty=DependencyProperty.Register(
“SelectedControl”,
类型(控制),
类型(开关控制),
新属性元数据(默认值(控制));
公共控制选择控制
{
get=>(控件)GetValue(SwitchingControl.SelectedControlProperty);
set=>SetValue(SwitchingControl.SelectedControlProperty,值);
}
//用于存储可重用控件的字典
专用字典控制映射{get;set;}
公共切换控制()
{
this.ControlMap=新字典()
{ 
{nameof(UserControlDashboard),new UserControlDashboard())}
};
}
//TODO::双击DataGrid行时调用
private void OnNewControlSelected(字符串selectedControlKey)
{
if(this.ControlMap.TryGetValue(selectedControlKey,out Control selectedControl)
{
this.SelectedControl=SelectedControl;
}
}
}

更高级的解决方案将涉及
DataTemplate
和不同的视图模型或数据模型,其中特定类型将映射到特定控件。然后,当模型添加到
ContentPresenter
时,将显示控件,该模型将自动应用正确的
DataTemplate
,以便可视化模型数据。

Ohh-wow谢谢你的超级详细的回答:))老实说,我仍然无法100%地理解它,但问题是我对C特定的语法(第一次是C)仍然不是很坚定。但我肯定会深入研究语法和您的解决方案,并尝试在我的项目中实现它,因为这看起来确实非常有希望。:)谢谢