如何使现有对象成为 Microsoft Expression Bend 3 中的数据源?

发布于 2024-08-05 09:25:21 字数 1038 浏览 3 评论 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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-08-12 09:25:21

您可以将任何 CLR 对象实例化为数据窗格中数据绑定的新数据源。

确保您想要使用的类的项目已经构建。

单击数据窗格右上角的图标,然后选择“定义新对象数据源”。这将允许您选择项目中的任何 CLR 类(我认为它必须有一个默认构造函数才符合条件)。该对象被包装到数据源中。

完成此操作后,该对象将出现在数据窗格中,并且可用于使用拖放或数据绑定对话框进行数据绑定(通过属性标记,即属性检查器中每个可绑定属性侧面的小矩形) 。

显然,要创建数据绑定列表,您可能希望您的对象成为事物的集合 - 我喜欢使用 ObervableCollection<>为此。

请注意,实例化为数据源的对象也可以在运行时从代码访问。为了查找您为其创建数据源的对象,请使用 FindResource 来搜索具有您最初创建数据源时指定的名称的数据源。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文