如何使用自定义RowStyle编辑WPF DataGrid中的行

如何使用自定义RowStyle编辑WPF DataGrid中的行

问题描述:

我需要使用 DataGrid 中的一些数据,使用每行的自定义 UserControl 。显示数据工作正常,但是当我编辑自定义 UserControl 中的字段时,绑定的数据记录不会更改。

I need to put some data in a DataGrid, using a custom UserControl for each row. Displaying the data works just fine, but when I edit the fields in the custom UserControl, the bound data records don't change.

如果我使用 ListBox 来显示数据,那么它都按预期工作。但是我宁愿使用DataGrid,它允许排序和(希望)添加新的记录。

If I use a ListBox to display the data instead, it all works as expected. But I would rather use the DataGrid, which allows for sorting and (hopefully) adding new records.

为了说明,这里是一个简单的数据类,我需要显示(编辑) - 个人及其配偶:

To illustrate, here is a simple data class that I need to display (and edit) - persons and their spouses:

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) => { };
}

..这里是自定义的UserControl(PersonView):

..and here is the custom UserControl (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>

最后,主窗口(代码隐藏)放在一起:

Finally, the main window (with code-behind) to put it all together:

<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中一样。

What can I do to make editing work in the DataGrid, as it does in the ListBox?

我的代码有点乱,我想我得到了

I twiddled with your code a bit and I think I got it working.

首先,您有一个关于您的PersonView的说明,指出只有在这些绑定使用UpdateSourceTrigger = PropertyChanged时,才能在DataGrid中进行编辑。我发现有必要设置UpdateSourceTrigger,但是我使用LostFocus尝试并保持与ListBox中相同的行为。

First of all, you have a note on your PersonView that says "Editing in DataGrid works only if these bindings use UpdateSourceTrigger=PropertyChanged." I found that it WAS necessary to set UpdateSourceTrigger, but I used LostFocus to try and keep the same behavior as is in the ListBox.

<!--Editing in DataGrid works only if these bindings use UpdateSourceTrigger-->
    <TextBox Text="{Binding Path=Name, UpdateSourceTrigger=LostFocus}" Width="70" />
    <TextBlock Text="is married to" Margin="6,0" Grid.Column="1" />
    <TextBox Text="{Binding Path=Spouse.Name, UpdateSourceTrigger=LostFocus}" Width="70" Grid.Column="2" />

其次,我使用了DataGridTemplateColumn,而不是使用DataGridTextColumns和DataGrid.RowStyle。我希望这是可以接受的 - 它似乎是你以前的方式,模板不是真的尊重列,但它现在。

Second, instead of using 2 DataGridTextColumns and DataGrid.RowStyle, I used a single DataGridTemplateColumn. I hope this is acceptable--it appears the way you had it before, the template wasn't really honoring the columns, but it does now.

        <DataGrid x:Name="_datagrid" Grid.Column="1" AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" >
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="People">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <local:PersonView />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

进行这些更改后,我看到双方更新。

After making these changes, I saw it updating on both sides.

我希望能帮助你。