INotifyPropertyChanged 不适用于传统绑定,但适用于编译绑定
INotifyPropertyChanged not working with traditonal binding but works with compiled binding
如果我做错了什么或者我遗漏了什么,我不会这样做,但是当我使用编译时绑定时 INotifyPropertyChanged 可以工作,而当我使用传统绑定时它不起作用。
public class Students : INotifyPropertyChanged
{
private string name;
private string surname;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Name
{
get { return name; }
set
{
if(value != name)
{
name = value;
NotifyPropertyChanged();
}
}
}
public string Surname
{
get { return surname; }
set
{
if (value != surname)
{
surname = value;
NotifyPropertyChanged();
}
}
}
public Students()
{
Name = "John";
Surname = "Smith";
}
}
MainPage.xaml
<Page.DataContext>
<local:Students/>
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Margin="0,200,0,0">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Surname}"/>
<Button Content="Change Name" Click="change_name"/>
<Button Content="Change Surname" Click="change_surname"/>
</StackPanel>
</Grid>
MianPage.xaml.cs
public sealed partial class MainPage : Page
{
Students st;
public MainPage()
{
this.InitializeComponent();
st = new Students();
}
private void change_name(object sender, RoutedEventArgs e)
{
st.Name = "MM";
}
private void change_surname(object sender, RoutedEventArgs e)
{
st.Surname = "SS";
}
}
我真的很困惑,因为当你使用编译时绑定进行绑定时,它工作正常。怎么回事?
我没有看到任何地方将当前 DataContext 设置为您的对象。
public MainPage()
{
this.InitializeComponent();
st = new Students();
this.DataContext = st;
}
或:您正在 XAML 中设置数据上下文,但并未引用它。
<Page.DataContext>
<local:Students/>
</Page.DataContext>
如果您打算使用该对象,则需要从代码中引用它。
private void change_name(object sender, RoutedEventArgs e)
{
((Students)this.DataContext).Name = "MM";
}
我遇到的情况与作者非常相似(如果不一样的话),我唯一要做的就是将我的视图模型设为 public 属性。在上面的例子中,它将 Students st;
更改为 public Students st {get; set;}
如果我做错了什么或者我遗漏了什么,我不会这样做,但是当我使用编译时绑定时 INotifyPropertyChanged 可以工作,而当我使用传统绑定时它不起作用。
public class Students : INotifyPropertyChanged
{
private string name;
private string surname;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Name
{
get { return name; }
set
{
if(value != name)
{
name = value;
NotifyPropertyChanged();
}
}
}
public string Surname
{
get { return surname; }
set
{
if (value != surname)
{
surname = value;
NotifyPropertyChanged();
}
}
}
public Students()
{
Name = "John";
Surname = "Smith";
}
}
MainPage.xaml
<Page.DataContext>
<local:Students/>
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Margin="0,200,0,0">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Surname}"/>
<Button Content="Change Name" Click="change_name"/>
<Button Content="Change Surname" Click="change_surname"/>
</StackPanel>
</Grid>
MianPage.xaml.cs
public sealed partial class MainPage : Page
{
Students st;
public MainPage()
{
this.InitializeComponent();
st = new Students();
}
private void change_name(object sender, RoutedEventArgs e)
{
st.Name = "MM";
}
private void change_surname(object sender, RoutedEventArgs e)
{
st.Surname = "SS";
}
}
我真的很困惑,因为当你使用编译时绑定进行绑定时,它工作正常。怎么回事?
我没有看到任何地方将当前 DataContext 设置为您的对象。
public MainPage()
{
this.InitializeComponent();
st = new Students();
this.DataContext = st;
}
或:您正在 XAML 中设置数据上下文,但并未引用它。
<Page.DataContext>
<local:Students/>
</Page.DataContext>
如果您打算使用该对象,则需要从代码中引用它。
private void change_name(object sender, RoutedEventArgs e)
{
((Students)this.DataContext).Name = "MM";
}
我遇到的情况与作者非常相似(如果不一样的话),我唯一要做的就是将我的视图模型设为 public 属性。在上面的例子中,它将 Students st;
更改为 public Students st {get; set;}