我正在开发一个WPF应用程序,其中包含一个DataGrid,其中包含姓名,姓氏,电子邮件等人员详细信息。
我使用ObservableCollection
列表DataSource
作为DataGrid
:
public ObservableCollection<Persons> personsLists = new ObservableCollection<Person>(Controller.SelectAllPersons());
接下来我想要实现的目标是:
例如,当我更改名字或姓氏时, 我将它保存到数据库,我想立即更新DataGrid。我尝试按照MSDN documentation:
实施INotifyPropertyChangedpublic class Person : MyInherit, INotifyPropertyChanged
{
#region Attributes
private string _fullName;
private int _townId;
private string _firstName;
private Town _town;
private string _lastName;
#endregion
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#region Properties
public Town Town
{
get { return _town; }
set
{
_town = value;
NotifyPropertyChanged("Town");
}
}
public int TownId
{
get { return _townId; }
set
{
_townId = value;
NotifyPropertyChanged("TownId");
}
}
public string FullName
{
get { return _fullName; }
set
{
_fullName = value;
NotifyPropertyChanged("FullName");
}
}
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
NotifyPropertyChanged("FirstName");
}
}
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
NotifyPropertyChanged("LastName");
}
}
}
这就是我在UI表单上编辑人物对象的地方:
private void btnPersonEdit_Click(object sender, System.Windows.RoutedEventArgs e)
{
Person selectedPerson = (Person)(dtgPersons.SelectedItem);
if (selectedPerson != null)
{
//GetExistingPerson from DataGrid (gettin obj from list which is source to a DataGrid so it's same as getting it from DataGrid)
var person = personsLists.Where(d => d.Id == selectedPerson.Id).FirstOrDefault();
if (person != null)
{
person.FirstName = txtFirstName.Text;
person.LastName = txtLastName.Text;
person.Town = (Town)cmbTowns.SelectedItem;
person.TownId = selectedPerson.Town.Id;
//Apply changes to a database
Person lastUpdated = Controller.UpdatePerson(person);
dtgPersons.UnselectAll();
}
}
//After this I thought the same item I edited should be updated immediately on a DataGrid but obviously I did something wrong
}
如何在不再设置源的情况下,使用此INotifyPropertyChanged接口更新DataGrid, 因为我觉得做这样的事情对性能不利:
dtg.ItemsSource=null;
dtg.ItemsSource= personsLists;
XAML:
<DataGrid Grid.Row="1" IsReadOnly="True" Name="dtgPersons" EnableColumnVirtualization = "True"
EnableRowVirtualization ="True" SelectionUnit="FullRow" Background="White" Margin="0,5,0,0"
AutoGenerateColumns="False" RowHeaderWidth="0" MaxWidth="4000" MaxHeight="2000" HorizontalGridLinesBrush="#0091EA"
VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" FontSize="{x:Static local:Globals.dataGridfontSizeContent}"
RowHeight="30" SelectionChanged="dtgPersons_SelectionChanged" >
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" CellStyle="{StaticResource DataGridCenteringLeft}"
Header="First Name" Foreground="Black" FontSize="16" FontFamily="Verdana" Width="*" />
<DataGridTextColumn Binding="{Binding LastName}" CellStyle="{StaticResource DataGridCenteringLeft}"
Header="Last Name" Foreground="Black" FontSize="16" FontFamily="Verdana" Width="*" />
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
为什么要在int
集合中查找Person
个对象?我想你想设置所选属性的属性:
listaselectedPerson