通过 XAML 绑定将自定义对象传递给 UserControl [英] Passing custom objects to UserControl via XAML binding

查看:21
本文介绍了通过 XAML 绑定将自定义对象传递给 UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是创建一个 UserControl,我可以向它传递一个 Address 对象.似乎当我将 Address="{Binding Path=Person.Address}" 传递给 UserControl 时,嵌入的 TextBox 绑定到 Text="{Binding Path=Person.Address}" 而不是 Text="{Binding Path=Address.Summary}"

What I'm trying to do is create a UserControl to which I can pass an Address object. It seems that when I pass Address="{Binding Path=Person.Address}" to the UserControl, the embedded TextBox is binding to Text="{Binding Path=Person.Address}" instead of Text="{Binding Path=Address.Summary}"

我的意思是不是全错了?

Am I going about this all wrong?

如果你想玩这个项目,这里有一个链接:http://dl.dropbox.com/u/4220513/WpfApplication2.zip

Here's a link to the project if you want to play with it: http://dl.dropbox.com/u/4220513/WpfApplication2.zip

域对象:

namespace WpfApplication2
{
    public class Person
    {
        public String Name { get; set; }
        public Address Address { get; set; }
    }

    public class Address
    {
        public String Street { get; set; }
        public String City { get; set; }

        public String Summary { get { return String.Format("{0}, {1}", Street, City); } }
    }
}

主窗口:

namespace WpfApplication2
{
    public partial class MainWindow : Window
    {
        private readonly ViewModel vm;
        public MainWindow()
        {
            InitializeComponent();
            vm = new ViewModel();
            DataContext = vm;

            vm.Person = new Person()
            {
                Name = "Bob",
                Address = new Address()
                {
                    Street = "123 Main Street",
                    City = "Toronto",
                },
            };
        }
    }

    public class ViewModel : INotifyPropertyChanged
    {
        private Person person;
        public Person Person { get { return person; } set { person = value; NotifyPropertyChanged("Person"); } }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBlock Text="Name:" />
        <TextBlock Text="{Binding Path=Person.Name}" />
        <TextBlock Text="Address:" />
        <local:AddressView Address="{Binding Path=Person.Address}" />
    </StackPanel>
</Window>

用户控件:

namespace WpfApplication2
{
    public partial class AddressView : UserControl
    {
        public AddressView()
        {
            InitializeComponent();
            DataContext = this;
        }

        public Address Address
        {
            get { return (Address)GetValue(AddressProperty); }
            set { SetValue(AddressProperty, value); }
        }

        public static readonly DependencyProperty AddressProperty =
            DependencyProperty.Register("Address", typeof(Address), typeof(AddressView));
    }
}

<UserControl x:Class="WpfApplication2.AddressView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBox Text="{Binding Path=Address.Summary}" IsReadOnly="True" />
</UserControl>

错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'Person' property not found on 'object' ''AddressView' (Name='')'. BindingExpression:Path=Person.Address; DataItem='AddressView' (Name=''); target element is 'AddressView' (Name=''); target property is 'Address' (type 'Address')

推荐答案

在 MainWindow.xaml 中:

In MainWindow.xaml :

<local:AddressView DataContext="{Binding Path=Person.Address}" />

然后在 AddressView.xaml 中

and then in AddressView.xaml

<TextBox Text="{Binding Path=Summary, Mode=OneWay}" IsReadOnly="True" />

这会为我显示摘要.

这篇关于通过 XAML 绑定将自定义对象传递给 UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆