为什么INotifyPropertyChanged的不更新在XAML中的变量? [英] Why is INotifyPropertyChanged not updating the variables in XAML?
问题描述
我想模拟数据的更改模型,并具有数据XAML中反映出来。据我了解,我需要实现INotifyPropertyChanged。
然而,在下面的代码示例,XAML从顾客显示数据仅一次,但日期及时间不会改变。
什么我要在下面的例子来改变,使XAML不断显示,因为它在模型改变了当前的日期和时间?
在特定的,我不明白是怎么绑定会知道什么时候检查模型,每一秒? 10倍的第二?没有任何事件,它是响应。缺少什么我在这里
XAML:
<窗口x:类=TestBinding99382.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X = http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:地方=CLR的命名空间:TestBinding99382
标题=窗口1HEIGHT =300WIDTH =300 >
< Window.Resources>
< ObjectDataProvider的
X:键=DataSourceCustomer
对象类型={X:键入地方:ShowCustomerViewModel}
方法名=GetCurrentCustomer/>
< /Window.Resources>
< DockPanel中的DataContext ={StaticResource的DataSourceCustomer}>
< StackPanel的DockPanel.Dock =评出的方向=横向>
< TextBlock的文本={绑定路径=姓}/>
< TextBlock的文本=/>
< TextBlock的文本={绑定路径=姓氏}/>
< / StackPanel的>
< StackPanel的DockPanel.Dock =评出的方向=横向>
< TextBlock的文本={绑定路径= TimeOfMostRecentActivity}/>
< / StackPanel的>
< / DockPanel中>
< /窗GT;
代码背后:
使用System.Windows;
使用System.ComponentModel;
使用系统;
命名空间TestBinding99382
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent() ;
}
}
//视图模型
公共类ShowCustomerViewModel:INotifyPropertyChanged的
{
私人客户_currentCustomer;
公共客户CurrentCustomer
{
得到
{
返回_currentCustomer;
}
组
{
_currentCustomer =价值;
this.RaisePropertyChanged(CurrentCustomer);
}
}
公共ShowCustomerViewModel()
{
_currentCustomer = Customer.GetCurrentCustomer();
}
公众客户GetCurrentCustomer()
{
返回_currentCustomer;
}
公共事件PropertyChangedEventHandler的PropertyChanged;
私人无效RaisePropertyChanged(字符串属性)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(属性));
}
}
}
//模型
公共类客户:INotifyPropertyChanged的
{
私人字符串_FirstName;
私人字符串_lastName;
私人的DateTime _timeOfMostRecentActivity;
公共字符串名字
{
得到
{
返回_FirstName;
}
组
{
_FirstName =价值;
this.RaisePropertyChanged(名字);
}
}
公共字符串姓氏
{
得到
{
返回_lastName;
}
组
{
_lastName =价值;
this.RaisePropertyChanged(姓氏);
}
}
公众的DateTime TimeOfMostRecentActivity
{
得到
{
返回_timeOfMostRecentActivity;
}
组
{
_timeOfMostRecentActivity =价值;
this.RaisePropertyChanged(TimeOfMostRecentActivity);
}
}
公共静态客户GetCurrentCustomer()
{
返回新客户
{名字=吉姆
,姓氏=史密斯
,TimeOfMostRecentActivity = DateTime.Now};
}
公共事件PropertyChangedEventHandler的PropertyChanged;
私人无效RaisePropertyChanged(字符串属性)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(属性));
}
}
}
}
也许我没有得到你希望这个代码做。
您已经创建了实现INotifyPropertyChanged一个Customer对象。您有另一个类,这是一个工厂的XAML去的实例。
现在创建具有预定义特性的客户实例。该视图显示它们。除非您更改属性不知何故,视图不会更新。
我添加了一个按钮,您的WPF查看
<按钮DockPanel.Dock =底
X:NAME =录入
点击=UpdateTime_Click>
更新活动时间戳
< /按钮>
C#代码隐藏:
私人无效UpdateTime_Click(对象发件人,RoutedEventArgs E)
{
Customer.GetCurrentCustomer()TimeOfMostRecentActivity = DateTime.Now。
}
我也改变了客户类型,为现有客户创建一个实例。
私有静态客户_CurrentCustomer;
公共静态客户GetCurrentCustomer()
{
如果(空== _CurrentCustomer)
{
_CurrentCustomer =新客户
{名字= 吉姆
,姓氏=史密斯
,TimeOfMostRecentActivity = DateTime.Now
};
}
返回_CurrentCustomer;
}
现在我每次点击该按钮时,的DateTime
属性被修改和视图自动更新由于 INotifyPropertyChanged的
机制。该代码似乎是工作AFAISee。
I want to simulate data changing in the model and having that data be reflected in XAML. As I understand I need to implement INotifyPropertyChanged.
However, in the following code example, XAML displays the data from the customer only once but the date and time never changes.
What do I have to change in the following example to make XAML continually display the current date and time as it changes in the model?
In particular, I don't understand how the Binding would know when to check the model, every second? 10 times a second? There is no event that it is responding to. What am I missing here?
XAML:
<Window x:Class="TestBinding99382.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestBinding99382"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider
x:Key="DataSourceCustomer"
ObjectType="{x:Type local:ShowCustomerViewModel}"
MethodName="GetCurrentCustomer"/>
</Window.Resources>
<DockPanel DataContext="{StaticResource DataSourceCustomer}">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Path=LastName}"/>
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Text="{Binding Path=TimeOfMostRecentActivity}"/>
</StackPanel>
</DockPanel>
</Window>
Code Behind:
using System.Windows;
using System.ComponentModel;
using System;
namespace TestBinding99382
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
//view model
public class ShowCustomerViewModel : INotifyPropertyChanged
{
private Customer _currentCustomer;
public Customer CurrentCustomer
{
get
{
return _currentCustomer;
}
set
{
_currentCustomer = value;
this.RaisePropertyChanged("CurrentCustomer");
}
}
public ShowCustomerViewModel()
{
_currentCustomer = Customer.GetCurrentCustomer();
}
public Customer GetCurrentCustomer()
{
return _currentCustomer;
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
//model
public class Customer : INotifyPropertyChanged
{
private string _firstName;
private string _lastName;
private DateTime _timeOfMostRecentActivity;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
this.RaisePropertyChanged("FirstName");
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
this.RaisePropertyChanged("LastName");
}
}
public DateTime TimeOfMostRecentActivity
{
get
{
return _timeOfMostRecentActivity;
}
set
{
_timeOfMostRecentActivity = value;
this.RaisePropertyChanged("TimeOfMostRecentActivity");
}
}
public static Customer GetCurrentCustomer()
{
return new Customer
{ FirstName = "Jim"
, LastName = "Smith"
, TimeOfMostRecentActivity = DateTime.Now};
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
Maybe I'm not getting what you wish this code to do.
You have created a Customer Object which implements INotifyPropertyChanged. You have another class which is a factory for the XAML to get to the instance. Now you create a Customer instance with predefined properties. The View displays them. Unless you change the properties somehow, the View will not update.
I added a button to your WPF View
<Button DockPanel.Dock="Bottom"
x:Name="UpdateTime"
Click="UpdateTime_Click">
Update Activity Timestamp
</Button>
C# code-behind:
private void UpdateTime_Click(object sender, RoutedEventArgs e)
{
Customer.GetCurrentCustomer().TimeOfMostRecentActivity = DateTime.Now;
}
I also changed Customer type to create a single instance for Current Customer
private static Customer _CurrentCustomer;
public static Customer GetCurrentCustomer()
{
if (null == _CurrentCustomer)
{
_CurrentCustomer = new Customer
{ FirstName = "Jim"
, LastName = "Smith"
, TimeOfMostRecentActivity = DateTime.Now
};
}
return _CurrentCustomer;
}
Now each time I click the button, the DateTime
Property is modified and the view auto-updates due to the INotifyPropertyChanged
mechanism. The code seems to be working AFAISee.
这篇关于为什么INotifyPropertyChanged的不更新在XAML中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!