我有这个特殊的问题。 我所拥有的只是我的XAML中的单个文本框,绑定到Person类。 当我在Person类中实现iNotifyPropertyChanged ,Visual Studio XAML设计器崩溃,如果我只是运行项目,我得到StackOverflow异常。
当我删除 iNotifyPropertyChanged一切正常,文本框绑定到Person类中的FirstName字段。
这是我的XAML,没什么特别的,只是一个数据绑定文本框
<Window x:Class="DataBinding_WithClass.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:c="clr-namespace:DataBinding_WithClass"> <Grid x:Name="myGrid" > <Grid.Resources> <c:Person x:Key="MyPerson" /> </Grid.Resources> <Grid.DataContext> <Binding Source="{StaticResource MyPerson}"/> </Grid.DataContext> <TextBox Text="{Binding FirstName}" Width="150px"/> </Grid> </Window>这是我的Person类,在同一个项目中:
public class Person: INotifyPropertyChanged { public string FirstName { get { return FirstName; } set { FirstName = value; OnPropertyChanged("FirstName"); } } public event PropertyChangedEventHandler PropertyChanged; // Create the OnPropertyChanged method to raise the event protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } }我努力了
重新启动Visual Studio 2012(在Windows 7 Home Premium 64Bit上运行)
开始一个新的空白项目 - 同样的问题
它很奇怪,没有iNotifyPropertyChanged一切都很好,但随后我的文本框不会得到更新,因为我的* Person *类中的FirstName发生了变化....
你有没遇到过这个问题?
I have this peculiar problem. All I have is a single textbox in my XAML, bound to a Person class. When I implement iNotifyPropertyChanged in Person class the Visual Studio XAML designer crashes and if i just run the project I get StackOverflow exception.
When I removeiNotifyPropertyChanged everything works fine and textbox gets bound to the FirstName field in Person class.
This is my XAML, nothing fancy, simply a databound textbox
<Window x:Class="DataBinding_WithClass.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:c="clr-namespace:DataBinding_WithClass"> <Grid x:Name="myGrid" > <Grid.Resources> <c:Person x:Key="MyPerson" /> </Grid.Resources> <Grid.DataContext> <Binding Source="{StaticResource MyPerson}"/> </Grid.DataContext> <TextBox Text="{Binding FirstName}" Width="150px"/> </Grid> </Window>This is my Person class, in the same project:
public class Person: INotifyPropertyChanged { public string FirstName { get { return FirstName; } set { FirstName = value; OnPropertyChanged("FirstName"); } } public event PropertyChangedEventHandler PropertyChanged; // Create the OnPropertyChanged method to raise the event protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } }I have tried
Restarting Visual Studio 2012(running on windows 7 Home Premium 64Bit)
Starting a new blank project - Same problem
Its so strange, without iNotifyPropertyChanged everything is fine , but then my textbox won't get updates as the FirstName in my *Person*class changes....
Have you come across this issue?
最满意答案
你不正确地实现了这个类。 你需要一个支持领域:
private string firstName; public string FirstName { get { return this.firstName; } set { if(this.firstName != value) { this.firstName = value; // Set field OnPropertyChanged("FirstName"); } } }现在,你的getter正在自我,setter设置属性本身,这两个都将导致StackOverflowException 。
You implemented the class improperly. You need a backing field:
private string firstName; public string FirstName { get { return this.firstName; } set { if(this.firstName != value) { this.firstName = value; // Set field OnPropertyChanged("FirstName"); } } }Right now, your getter is getting itself, and the setter sets the property itself, both of which will cause a StackOverflowException.
更多推荐
发布评论