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.

4
  • If you are a really new one in .NET you should begin your way with some books or tutorials on C# and WPF. Jun 28, 2014 at 19:23
  • For now your question looks like a duplicate of this and this one, also there is codeproject. If you can elaborate, describe your scenario, provide some code(XAML and code-behind) then it may turn another way. Jun 28, 2014 at 19:24
  • Because otherwise your question is a bit broad to be answered. Provide some context at least. Jun 28, 2014 at 19:25
  • I stumbled across this stackoverflow.com/questions/5065561/… 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 Jun 28, 2014 at 19:50

1 Answer 1

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

1
  • Just the last line should be StkPanel.Children.Add((UIElement)o); and the whole lines after the InitializeComponent should be in an eventhandler 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. Jun 29, 2014 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.