0

这个例子只是为了学习......

我已经在 Visual Studio C# 中启动了一个项目。很简单,后面的代码中实例化了一个Phone类。我想使用 Blend 3 添加 GUI。

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));

}

后面的代码:

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

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

我现在将这个项目导入到 Blend 3 中,这样我就可以添加一个图形元素并绑定到 Nokia 对象的 PhoneMake 属性。

如果我单击添加实时数据源按钮,我只能选择实例化一个新对象,我看不到如何选择我的诺基亚对象。

如何将此实例化对象诺基亚设置为数据源?

Blend应该能够做到这一点还是我把整个事情都弄错了?

使用 Visual Studio C# Express 2008 和 Blend 3。

4

1 回答 1

1

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.

于 2009-11-26T09:07:03.197 回答