Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7809551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:28:30+00:00 2026-06-02T03:28:30+00:00

Im trying to create a button from code behind that has the same attributes

  • 0

Im trying to create a button from code behind that has the same attributes as this:

<Button Content="Find Student" Tag="FindStudent" HorizontalAlignment="Right" x:Name="btnFindStudent" Click="btnGeneral_Click" />

Code behind:

            Button btnFindStudent = new Button();
            btnFindStudent.Click += this.btnGeneral_Click;
            btnFindStudent.Name = Convert.ToString("btnFindStudent");
            btnFindStudent.Tag = Convert.ToString("FindStudent");
            btnFindStudent.Content = Convert.ToString("View");
            btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
            btnFindStudent.Height = 20;
            btnFindStudent.Width = 36;

The problem is when I click the button I get an error

Value cannot be null.
Parameter name: Children of 'System.Windows.Controls.UIElementCollection' cannot be null. Object derived from UIElement expected.

And im not quite sure whats missing, this is the full code:

public partial class FindStudent : UserControl
{
    private Dictionary<string, UserControl> _userControls = new Dictionary<string, UserControl>();
    public Dictionary<string, UserControl> GetUserControls()
    {
        return _userControls;
    }
    private static readonly Random rand = new Random();
    public FindStudent()
    {
        InitializeComponent();

        List<string> userControlKeys = new List<string>();
        userControlKeys.Add("FindStudent");
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        foreach (string userControlKey in userControlKeys)
        {
            string userControlFullName = String.Format("{0}.AppPages.{1}", type.Namespace, userControlKey);
            UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
            _userControls.Add(userControlKey, userControl);
        }
        for (int i = 1; i <= 10; i++)
        {
            Button btnFindStudent = new Button();
            btnFindStudent.Click += this.btnGeneral_Click;
            btnFindStudent.Name = Convert.ToString("btnFindStudent");
            btnFindStudent.Tag = Convert.ToString("FindStudent");
            btnFindStudent.Content = Convert.ToString("View {0}", i);
            btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
            btnFindStudent.Height = 20;
            btnFindStudent.Width = 36;

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(btnFindStudent);
            MainArea.Children.Add(stackPanel);
        }
    }
    private void btnGeneral_Click(object sender, RoutedEventArgs e)
    {
        PanelMainContent.Children.Clear();
        Button button = (Button)e.OriginalSource;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;

        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
        // error on above line. 
    }
}

Xaml:

<UserControl x:Class="WpfApplication4.AppPages.FindStudent"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DockPanel HorizontalAlignment="Stretch" >
        <ScrollViewer VerticalScrollBarVisibility="Hidden">

            <DockPanel x:Name="PanelMainWrapper" Margin="10,20,0,0">
                <DockPanel x:Name="PanelMainContent" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,30,0,0">
                    <ScrollViewer VerticalScrollBarVisibility="Hidden">
                        <WrapPanel x:Name="MainArea" VerticalAlignment="Bottom" HorizontalAlignment="Left">

                        </WrapPanel>
                    </ScrollViewer>
                </DockPanel>
            </DockPanel>

        </ScrollViewer>
    </DockPanel>
</UserControl>

It should just reload the page I am currently using and I can set the appPage at a later date, but it doesnt want to play ball. This works in my main window just not on my control but I dont set the button from code behind I use the xaml I first mention in my main window. Which tells me I am missing something from button click (I think is set correct), tag is set correctly but I am not sure name will be x:Name like my xaml code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:28:31+00:00Added an answer on June 2, 2026 at 3:28 am

    Although your code really confuses me, here is what isn’t working. Your constructing a fully qualified type name by

    String.Format("{0}.AppPages.{1}", type.Namespace, userControlKey)
    

    where type.Namespace already contains the complete namespace of the type in question and hence the “AppPages” part is needless. Instead, you should write

    String.Format("{0}.{1}", type.Namespace, userControlKey)
    

    And why not simply do

    assembly.CreateInstance(type.FullName);
    

    when you only want to create a new instance of FindStudent (which i guess is what you want to do).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple button that deletes a row from the database.
I'm trying to create the effect of a button that 'lifts' from the page
I'm trying to create a transparent (no button background) ImageButton that has a custom
I'm trying to create a perfectly circular button, So I created a template from
I am trying to create a button that can pop up the speech input
I'm trying to create a post button that inserts the latest posts into a
I am trying to create a delete button over each image that appears in
I am trying to create a button on an <a href=# ...> tag using
I am trying to create Anchors in code behind and delete buttons as well
I am trying to create a button on my access form that allows for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.