We help IT Professionals succeed at work.
Get Started

Beginner -- how to make a XAML User Control Bindable

VoodooFrog asked
Last Modified: 2012-05-07
I am making a simple demo to learn how to create a bindable user control.  I have created a simple class:  

class Person
        public string firstName;
        public string lastName;

        public Person(string first, string last)
            firstName = first;
            lastName = last;

Open in new window

I created a very simple user control:  

<UserControl x:Class="Example.ExampleHRControl"
             d:DesignHeight="300" d:DesignWidth="300">
        <TextBlock x:Name="textFirstName"></TextBlock>
        <TextBlock x:Name="textLastName"></TextBlock>

Open in new window

What I would like to know is what do I need to do in order to be able to use the user control in context like a normal control.  I can add this to the MainWindow like normal:

<local:ExampleHRControl x:Name="Hr1"></local:ExampleHRControl>

Open in new window

and then I can address it through c# and add the value:

Hr1.textFirstName.Text = "John";
Hr1.textLasttName.Text = "Doe";

Open in new window

I would prefer to be able to create an instance of the class Person and simple bind the control on the main window to the Person class.
Watch Question
Director of Information Technology
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE