Toner
Toner

Reputation:

WPF: adding a usercontrol to a window

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.

Let me explain my problem.

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

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

Window.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.

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

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

Upvotes: 2

Views: 25712

Answers (4)

Mark Homer
Mark Homer

Reputation: 1035

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

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

[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

Upvotes: 0

Base33
Base33

Reputation: 3215

At the top of the window xaml add

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.

Upvotes: 3

Jeff Wain
Jeff Wain

Reputation: 1022

In your window, it sounds like you need to add the event to the instances of 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.

Let me know if you need further clarification.

Upvotes: 0

MrTelly
MrTelly

Reputation: 14875

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

Upvotes: 0

Related Questions