我需要将一些数据放入 a 中DataGrid
,对每一行使用自定义UserControl
。显示数据工作得很好,但是当我编辑 custom 中的字段时UserControl
,绑定的数据记录不会改变。
如果我使用 aListBox
来显示数据,则一切都按预期工作。但我宁愿使用 DataGrid,它允许排序和(希望)添加新记录。
为了说明,这是一个我需要显示(和编辑)的简单数据类 - 人和他们的配偶:
public class PersonViewModel : INotifyPropertyChanged
{
public PersonViewModel(string name)
{
_name = name;
}
private string _name = null;
public string Name
{
get { return _name; }
set
{
_name = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
private PersonViewModel _spouse = null;
public PersonViewModel Spouse
{
get { return _spouse; }
set
{
_spouse = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("Spouse"));
}
}
public event PropertyChangedEventHandler PropertyChanged = (s, e) => { };
}
..这是自定义用户控件(PersonView):
<UserControl x:Class="TestDataGrid.PersonView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--Editing in DataGrid works only if these bindings use UpdateSourceTrigger=PropertyChanged-->
<TextBox Text="{Binding Path=Name}" Width="70" />
<TextBlock Text="is married to" Margin="6,0" Grid.Column="1" />
<TextBox Text="{Binding Path=Spouse.Name}" Width="70" Grid.Column="2" />
</Grid>
</UserControl>
最后,将主窗口(带有代码隐藏)放在一起:
<Window x:Class="TestDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDataGrid"
Title="MainWindow" Height="350" Width="500">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--Changes made here correctly update the persons' names,
and the changes are reflected in the DataGrid below-->
<ListBox x:Name="_listbox" >
<ListBox.ItemTemplate>
<DataTemplate>
<local:PersonView />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--Changes made here never reach the PersonViewModels..-->
<DataGrid x:Name="_datagrid" Grid.Column="1"
AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" />
<DataGridTextColumn Binding="{Binding Path=Spouse.Name}" Header="Spouse" />
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<local:PersonView />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Grid>
</Window>
...
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var personA = new PersonViewModel("Alice");
var personB = new PersonViewModel("Barry");
var personC = new PersonViewModel("Carl");
var personD = new PersonViewModel("Doris");
personA.Spouse = personB;
personC.Spouse = personD;
var persons = new List<PersonViewModel>() { personA, personC };
_listbox.ItemsSource = persons;
_datagrid.ItemsSource = persons;
}
}
我可以做些什么来在 DataGrid 中进行编辑,就像在 ListBox 中一样?