Beginner -- how to make a XAML User Control Bindable

VoodooFrog
VoodooFrog used Ask the Experts™
on
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"
             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">
    <Grid>
        <TextBlock x:Name="textFirstName"></TextBlock>
        <TextBlock x:Name="textLastName"></TextBlock>
    </Grid>
</UserControl>

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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle AbrahamsSenior .Net Developer

Commented:
http://stackoverflow.com/questions/679933/wpf-binding-multiple-controls-to-different-datacontexts

Set up your dataContext for your grid, and then Set the text to bind to the appropriate field.

Author

Commented:
I'm assuming that the user control needs to have some modifications to allow it to be bindable?  The user control had no code behind right now =D
Senior .Net Developer
Commented:
Yeah.  

For a full example see this:
http://aworks.codeplex.com/

with an explination video here:
http://www.xneterp.com/vid/aworks/Aworks.html

Essentially you'll get the data from somewhere, and set the context as a resource which can be used.

Another great resource:
http://channel9.msdn.com/Tags/silverlight

See all the silverlight episodes.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial