积极答复者
关于数据绑定到CLR中的问题

问题
-
在做一个数据绑定到CLR中的工程时,发现CLR中的数据无法绑定到element上,代码具体如下
public partial class MainWindow : Window { Person person; Name name; Address addr; public MainWindow() { InitializeComponent(); name =new Name( "Jack", "Wang" ); addr = new Address( "Work", "Redmond", "One Microsoft Way", "WA", "950853" ); person = new Person(name, addr); DataContext = person; } Person.cs public class Person { Name name; Address address; public Person(Name nam, Address addr) { name = nam; address = addr; } } Name.cs public class Name:INotifyPropertyChanged { private string first; private string First { get { return first; } set { first = value; NotifyChanged("First"); } } public string second; public string Second { get { return second; } set { second = value; } } public Name(string first, string second) { First = first; Second = second; } public event PropertyChangedEventHandler PropertyChanged; void NotifyChanged(string property) { if(PropertyChanged!=null) PropertyChanged(this,new PropertyChangedEventArgs(property)); } } Addresses.cs public class Address { public string addressName; public string AddressName { get { return addressName; } set { addressName = value; } } public string city; public string City { get { return city; } set { city = value; } } public string state; public string State { get { return state; } set { state = value; } } public string street; public string Street { get { return street; } set { street = value; } } public string zip; public string Zip { get { return zip; } set { zip = value; } } public Address(string addname, string city, string state, string street, string zip) { AddressName = addname; City = city; State = state; Street = street; Zip = zip; }
MainWindow.XAML
<Window x:Class="WpfDateBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:WpfDateBinding" Title="MainWindow" Height="350" Width="525"> <StackPanel> <TextBlock Height="23" Name="TextBlock1" Text="{Binding Path=Name.First, Mode=OneWay}" /> <TextBlock Height="23" Name="TextBlock2" Text="{Binding Path=Name.Second,Mode=OneWay}" />
</StackPanel> </Window>
- 已编辑 conarena 2011年6月7日 7:21
答案
-
WPF中的绑定支持的是 CLR 属性,也就是有Set和Get访问器。但是你的Person类中,Name不是属性,所以绑定无效。
除了这个你还有几个错误,一是要注意大小写;二是要注意被绑定的属性必须是Public的,在C#中不写访问修饰符,默认是internal。
下面是正确的代码:
<StackPanel> <TextBlock Height="23" Name="TextBlock1" Text="{Binding Path=name.First, Mode=OneWay}" /> <TextBlock Height="23" Name="TextBlock2" Text="{Binding Path=name.Second,Mode=OneWay}" /> <!-- 注意name的大小写 --> </StackPanel>
C#:
public class Person { public Name name { get; set; } public Address address { get; set; } // 注意需要是属性,而且加上public public Person(Name nam, Address addr) { name = nam; address = addr; } }
public class Name : INotifyPropertyChanged { private string first; public string First { get { return first; } set { first = value; NotifyChanged("First"); } } private string second; public string Second { get { return second; } set { second = value; } } //注意访问修饰符 ......
Sincerely,
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 conarena 2011年6月7日 8:05
全部回复
-
WPF中的绑定支持的是 CLR 属性,也就是有Set和Get访问器。但是你的Person类中,Name不是属性,所以绑定无效。
除了这个你还有几个错误,一是要注意大小写;二是要注意被绑定的属性必须是Public的,在C#中不写访问修饰符,默认是internal。
下面是正确的代码:
<StackPanel> <TextBlock Height="23" Name="TextBlock1" Text="{Binding Path=name.First, Mode=OneWay}" /> <TextBlock Height="23" Name="TextBlock2" Text="{Binding Path=name.Second,Mode=OneWay}" /> <!-- 注意name的大小写 --> </StackPanel>
C#:
public class Person { public Name name { get; set; } public Address address { get; set; } // 注意需要是属性,而且加上public public Person(Name nam, Address addr) { name = nam; address = addr; } }
public class Name : INotifyPropertyChanged { private string first; public string First { get { return first; } set { first = value; NotifyChanged("First"); } } private string second; public string Second { get { return second; } set { second = value; } } //注意访问修饰符 ......
Sincerely,
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 conarena 2011年6月7日 8:05