0

I basically just started learning C# like a few days ago and Im trying to create a small application. I am coming from a PHP background and pretty much know nothing about the .NET framework. Now with my disclaimer out of the way and apologies if I slaughter some terms and such.

So, I have a button and instead of creating several functions to do the same thing I figured I can create one function and assign the same click function to each of my buttons to load a new User Control in my WPF application. Pretty much I have will have several buttons that are going to load the same thing:

<Button Content="Link1" Name="UserControlNameToLoad" Click="MyEventToLoad" />

So when I click this button I am trying to get the User Control which is the same name as the name in the button.

Any help is greatly appreciated and once again sorry if I sound real n00bish here since I pretty much am.

KyleMassacre
  • 362
  • 3
  • 14
  • If you are a really new one in .NET you should begin your way with some books or tutorials on [C#](http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx) and [WPF](http://www.wpftutorial.net/WPFIntroduction.html). – Eugene Podskal Jun 28 '14 at 19:23
  • For now your question looks like a duplicate of [this](http://stackoverflow.com/questions/1774962/wpf-how-do-i-load-user-controls-dynamically) and [this one](http://stackoverflow.com/questions/8677500/wpf-mvvm-load-an-usercontrol-at-runtime), also there is [codeproject](http://www.codeproject.com/Tips/665546/Passing-Command-Parameter-for-Buttons-within-an). If you can elaborate, describe your scenario, provide some code(XAML and code-behind) then it may turn another way. – Eugene Podskal Jun 28 '14 at 19:24
  • Because otherwise your question is a bit broad to be answered. Provide some context at least. – Eugene Podskal Jun 28 '14 at 19:25
  • I stumbled across this http://stackoverflow.com/questions/5065561/how-can-i-load-a-usercontrol-by-specifying-a-string-value but just had to make a couple changes to get this work but it does now. I will admit I'm not much for reading first I'm more of a try it out kind of person and if I get stuck then I'll read and read and read till I find my answer. This was my last resort though and thanks – KyleMassacre Jun 28 '14 at 19:50

1 Answers1

1

Here is what I did which came from here How can I load a UserControl by specifying a string value?

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button_clicked(object sender, RoutedEventArgs e)
        {
            var btn = (Button) sender;
            Type newType = Type.GetType("WpfApplication1."+ btn.Name, true, true);

            object o = Activator.CreateInstance(newType);
            StkPanel.Children.Add((UIElement) o);
        }
    }
}

Thank you for your help

Community
  • 1
  • 1
KyleMassacre
  • 362
  • 3
  • 14
  • Just the last line should be StkPanel.Children.Add((UIElement)o); and the whole lines after the InitializeComponent should be in an eventhandler http://msdn.microsoft.com/en-us/library/edzehd2t(v=vs.110).aspx - double click on button in editor to autogenerate the stub, then use the Click event property in other buttons to set the same handler for them. – Eugene Podskal Jun 29 '14 at 03:04