views:

188

answers:

1

This example is just for learning ...

I have started a project in Visual Studio C#. It is very simple, there is a Phone class which is instantiated in the code behind. I would like to add the GUI using Blend 3.

public class Phone:DependencyObject
{
    public string PhoneMake
        {
            get { return (string)GetValue(PhoneMakeProperty); }
            set { SetValue(PhoneMakeProperty, value); }
        }

        public static readonly DependencyProperty PhoneMakeProperty =
            DependencyProperty.Register("PhoneMake", typeof(string), typeof(Phone));

}

The code behind:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Phone Nokia = new Phone();
        Nokia.PhoneMake = "Nokia";
    }
}

I now import this project into Blend 3 so I can add a graphic element and bind to the PhoneMake property of the Nokia object.

If I click the Add live data source button I am only given the option to instantiate a new object, I can't see how to select my Nokia object.

How do I set this instantiated object Nokia as a data source?

Should Blend be able to do this or have I got the whole thing wrong?

Using Visual Studio C# Express 2008 and Blend 3.

A: 

You can instantiate any CLR object as a new data source for data binding in the Data pane.

Make sure your project with the class you want to use has been built.

Click the icon in the upper right corner of the data pane and select Define New Object Data Source. This will let you pick any CLR class in your project (I think it must have a defualt constructor to be eligible). The object is wrapped into a data source.

Once you have done this, the object appears in the data pane and can be used for data binding using drag and drop or the data binding dialog (via the property marker, the little rectangle at the side of each bindable property in the property inspector).

Obviously, to create data bound lists, you probably want your object to be a collection of things - I like to use ObervableCollection<> for that.

Note that your object instantiated as a data source is accessible from code as well at runtime. In order to find the object you created the data source for, use FindResource to search for the data source with the name you gave it when you created it originally.

Christian Schormann