从DataGrid的已编辑单元格返回null

时间:2017-04-19 18:14:32

标签: c# wpf datagrid datagridcell

我在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>

2 个答案:

答案 0 :(得分:1)

您正在设置&#34;文件夹&#34;您的产品的属性为null然后您再也不会设置它。您只是在更改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;
}

答案 1 :(得分:0)

您应该设置相应folder对象的Product属性,而不是设置Text的{​​{1}}属性:

TextBlock

请注意,为了能够在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接口,并在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));
    }
}