Returns DataGrid 的已编辑单元格中为空

Returns null from edited cell of DataGrid

我在 WPF 中创建了一个带有可编辑单元格的简单 DataGrid。但是我从编辑的单元格中获取空数据。我是 WPF 的新手。有什么问题?

当我尝试更改文件夹单元格时,它发生了变化,如图所示 in the image.

这是 result!

代码,

namespace MyApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Product> tmpProducts = new List<Product>();

            tmpProducts.Add(new Product {
                id = 1, title = "Product A", folder = null,
            });
            tmpProducts.Add(new Product {
                id = 2,  title = "Product B", folder = null,
            });

            productGrid.ItemsSource = tmpProducts;
        }

        private void selectFolder(object sender, RoutedEventArgs e)
        {
            // selected folder from dialog, etc. then,
            string selectedPath = "C:\myFolder";

            object item = productGrid.SelectedItem;
            (productGrid.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text = selectedPath;
        }

        private void updateProducts(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < productGrid.Items.Count; i++)
            {
                Product prd = (Product)productGrid.Items[i];
                Console.WriteLine(prd.folder);
            }
        }
    }


    class Product
    {
        public int id { get; set; }
        public string title { get; set; }
        public string folder { get; set; }
    }
}
<Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="productGrid" AutoGenerateColumns="False" CanUserAddRows="False" Margin="0,0,0,36">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding id}" Header="ID"/>
                <DataGridTextColumn Binding="{Binding title}" Header="Title" Width="*"/>
                <DataGridTextColumn Binding="{Binding folder}" Header="Foler" Width="*"/>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Click="selectFolder" Content="Browse.." Width="100"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Button Click="updateProducts" Content="Save" HorizontalAlignment="Left" Margin="432,289,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

您正在将产品的 "folder" 属性 设置为空,然后您再也不会设置它。您只是在更改 DataGridCell 的文本。在尝试在 updateProducts 事件中获取它之前,您还需要设置产品的文件夹 属性。像这样:

private void selectFolder(object sender, RoutedEventArgs e)
{
    // selected folder from dialog, etc. then,
    string selectedPath = "C:\myFolder";

    object item = productGrid.SelectedItem;

    (item as Product).folder = selectedPath; //set the folder property of your Product item

    (productGrid.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text = selectedPath;
}

你应该设置相应Product对象的folder属性而不是设置TextBlockText属性:

private void selectFolder(object sender, RoutedEventArgs e)
{
    Button btn = sender as Button;
    Product product = btn.DataContext as Product;
    string selectedPath = "C:\myFolder";
    product.folder = selectedPath;
}

请注意,为了让您能够在 DataGrid 中看到新值,Product class 应该实现 INotifyPropertyChanged 接口并引发 PropertyChanged 文件夹 setter 中的事件 属性:

class Product : INotifyPropertyChanged
{
    public int id { get; set; }
    public string title { get; set; }

    private string _folder;
    public string MyProperty
    {
        get { return _folder; }
        set { _folder = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}