如何让我的Partial Class更新 [英] How can I get my Partial Class to update
问题描述
public partial class Employee
{
public string FullName
{
get {return LastName.Trim()+,+ FirstName.Trim();
}
}
我遇到的问题是我的编辑表单文本框绑定到LastName和FirstName属性。当我更新诸如CompanyName,LastName,FirstName,HireDate等的值时,DataGrid中所有被更改的列都被正确更新,但是FullName属性没有被更新。
为什么我的FullName partial类不会更新,而其余的DataGrid属性更新?如何更正?
这是我的editform xaml。
< TextBox Grid.Column =1Name =txtCompanyText ={Binding SelectedEmployee.Company,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}/>
< TextBox Grid.Column =1Name =txtFirstNameText ={Binding SelectedEmployee.FirstName,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}/>
< TextBox Grid.Column =1Name =txtLastNameText ={Binding SelectedEmployee.LastName,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}/>
< TextBlock Grid.Column =1Name =txtFullNameGrid.Row =2Margin =2Text ={Binding SelectedEmployee.FullName/> - 在DataGrid中显示为列
更改通知在WPF中使用 INotifyPropertyChanged 界面完成。
它与Entity Framework一起使用,因为生成的实体(由默认的edmx模板)实现此接口,并且生成的属性设置程序触发 PropertyChanged
事件。
您的新物业不会提高此事件,但由于它是根据 LastName
和 FirstName
当这两个变化你需要提高 PropertyChanged
事件。
幸运EF将会在每个属性设置器中生成一个名为 OnSomePropertyChanged
的属性设置器,从中可以提出事件:
public partial class Employee
{
public string FullName
{
get {return LastName.Trim()+,+ FirstName.Trim ); }
}
partial void OnLastNameChanged()
{
base.OnPropertyChanged(FullName);
}
partial void OnFirstNameChanged()
{
base.OnPropertyChanged(FullName);
}
}
I have a WPF app that uses an Entity Framework Model. One of the Entities is an Employee Type. This entity has a LastName and FirstName property among others. But in my xaml, I want to display a full name in my DataGrid. So I created this Partial Class to return the full name and I display this property in the DataGrid. This works great. The FullName is displayed in the DataGrid as expected.
public partial class Employee
{
public string FullName
{
get { return LastName.Trim() + ", " + FirstName.Trim(); }
}
}
The problem I have is that my edit form has textboxes bound to the LastName and FirstName properties. When I update values such as CompanyName, LastName, FirstName, HireDate, etc... all the columns in the DataGrid that were changed get updated correctly, but the FullName property does NOT get updated.
Why doesn't my FullName partial class get updated while the rest of the DataGrid properties do get updated? How do I correct this?
Here is my editform xaml.
<TextBox Grid.Column="1" Name="txtCompany" Text="{Binding SelectedEmployee.Company, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Column="1" Name="txtFirstName" Text="{Binding SelectedEmployee.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Column="1" Name="txtLastName" Text="{Binding SelectedEmployee.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Column="1" Name="txtFullName" Grid.Row="2" Margin="2" Text="{Binding SelectedEmployee.FullName" /> -- Also show as column in DataGrid
The change notification in WPF is done with the INotifyPropertyChanged interface.
It works out of the box with Entity Framework because the generated entities (by the default edmx template) implement this interface and the generated property setters fire the PropertyChanged
event.
Your new property is not raising this event, but because it's calculated from LastName
and FirstName
when those two change you need to raise the PropertyChanged
event.
Luckily EF will generate a partial method into each property setter with the name OnSomePropertyChanged
from where you can raise the event:
public partial class Employee
{
public string FullName
{
get { return LastName.Trim() + ", " + FirstName.Trim(); }
}
partial void OnLastNameChanged()
{
base.OnPropertyChanged("FullName");
}
partial void OnFirstNameChanged()
{
base.OnPropertyChanged("FullName");
}
}
这篇关于如何让我的Partial Class更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!