C# WPF:向窗口添加用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/771910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:31:16  来源:igfitidea点击:

WPF: adding a usercontrol to a window

c#wpfuser-controls

提问by

First off, I'm new to WPF and C# so maybe the issue I have is really easy to fix. But I'm kinda stuck at the moment.

首先,我是 WPF 和 C# 的新手,所以也许我遇到的问题很容易解决。但我现在有点卡住了。

Let me explain my problem.

让我解释一下我的问题。

I have a WPF Window and two usercontrols (Controls and ContentDisplayer).

我有一个 WPF 窗口和两个用户控件(控件和 ContentDisplayer)。

The usercontrol Controls, wich contains some buttons, is added in the XAML of the Window. Nothing special here.

包含一些按钮的用户控件控件被添加到窗口的 XAML 中。这里没什么特别的。

Window.XAML

窗口.XAML

<nv:Controls/>

Now, what I want to do is when a user is pressing a button in Controls, ContentDisplayer needs to be added to the Scatterview I have in my Window.

现在,我想要做的是当用户按下控件中的按钮时,需要将 ContentDisplayer 添加到我的窗口中的 Scatterview 中。

I solved the problem by adding the buttons to the Window, and not using the usercontrol Controls. But this is not what I want.

我通过将按钮添加到窗口解决了这个问题,而不是使用用户控件控件。但这不是我想要的。

Window.XAML.CS

窗口.XAML.CS

private static void Button_ContactChanged(object sender, ContactEventArgs e)
    {
      object ob = Application.LoadComponent(new Uri(
      "NVApril;component\XAML\ContentDisplayer.xaml",
      System.UriKind.RelativeOrAbsolute));

    //Set a unique name to the UserControl
      string name = String.Format("userControl{0}",
      SurfaceWindow1_Scatterview.Items.Count);
      UserControl userControl = ob as UserControl;
      userControl.Name = name;

    //Add the new control to the Scatterview
      SurfaceWindow1_Scatterview.Items.Add(userControl);
      SurfaceWindow1_Scatterview.RegisterName(name, userControl);
    }

So the real question is: How do I add a usercontrol to the Window by pressing a button in an other usercontrol?

所以真正的问题是:如何通过按下其他用户控件中的按钮将用户控件添加到窗口?

Thanks,

谢谢,

Toner

碳粉

回答by MrTelly

Within Controls expose an event that is fired when you want to add a new control.

在控件中公开一个事件,当您想要添加新控件时会触发该事件。

public event EventHandler AddControl;

private void RaiseAddControl()
{
    if (AddControl!= null)
    {
        AddControl(this, EventArgs.Empty);
    }
}

Now sink that event in your Window

现在在您的窗口中接收该事件

yourControl.AddControl += ContactChanged

回答by Jeff Wain

In your window, it sounds like you need to add the event to the instances of Controls.

在您的窗口中,听起来您需要将事件添加到 Controls 的实例中。

<local:ContentDisplayer>
...
  <nv:Controls AddControl="ContactChanged"/>
...

Then in your ContactChanged event handler you can instantiate a new Controls control and add it to whatever collection you're using like in your Button_ContactChanged event handler above.

然后在您的 ContactChanged 事件处理程序中,您可以实例化一个新的 Controls 控件并将其添加到您正在使用的任何集合中,就像在上面的 Button_ContactChanged 事件处理程序中一样。

Let me know if you need further clarification.

如果您需要进一步说明,请告诉我。

回答by Base33

At the top of the window xaml add

在窗口顶部 xaml 添加

xmlns:d="clr-namespace:SomeNamespace.Usercontrols"

where you these exist already, you can choose the namespace of your control from the intellesence list.

如果这些已经存在,您可以从智能列表中选择控件的命名空间。

Then where you want to place the control type:

然后你想放置控件类型的位置:

<d:yourusercontrolhere params />

and your usercontrols can be added there.

并且您的用户控件可以添加到那里。

回答by Mark Homer

I have no idea what you are trying to do your example,

我不知道你想做什么你的例子,

So you have a control defined thus:

所以你有一个这样定义的控件:

public partial class somecontrolname : UserControl

With your corresponding Xaml file

使用相应的 Xaml 文件

All you need to do to add it in code to your window is firstly you need a LayoutRoot such as Grid control in the window then just

要将它以代码形式添加到窗口中,您需要做的就是首先在窗口中需要一个 LayoutRoot,例如 Grid 控件,然后只需

[mylayoutcontrol].Children.Add(new somecontrolname());

[mylayoutcontrol].Children.Add(new somecontrolname());

Maybe I got wrong idea what you are trying to do, your example code doesn't make much sense to me, looks like you are trying to load the xaml source file

也许我弄错了您要做什么,您的示例代码对我来说没有多大意义,看起来您正在尝试加载 xaml 源文件