在 DataGrid (WPF) 中编辑单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13906826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:38:06  来源:igfitidea点击:

Edit Cell in DataGrid (WPF)

c#wpfdatagriddatagridviewwpfdatagrid

提问by Ricardo Polo Jaramillo

I am new in WPF. I used to work in Winforms.

我是 WPF 的新手。我曾经在 Winforms 工作过。

In Winforms I had the DataGridView that allows me to change, when I want a cell value.

在 Winforms 中,我有 DataGridView 允许我在需要单元格值时进行更改。

Simply using:

简单地使用:

dataGridView[columnIndex, rowIndex].Value = "New Value";

It works.

有用。

How can I accomplish this using DataGrid from WPF? I was looking thorught stack over flow and could figure out an easy way to do this.

如何使用 WPF 中的 DataGrid 完成此操作?我正在考虑堆栈溢出,并且可以找到一种简单的方法来做到这一点。

Thank you

谢谢

采纳答案by sa_ddam213

Ok the simplest way to handle DataGridis by binding to an ItemSource.

好的,最简单的处理方法DataGrid是绑定到ItemSource.

The example below shows how to bind your list and how changes upadte the DataGrid.

下面的示例显示了如何绑定列表以及更改如何更新 DataGrid。

public partial class MainWindow : Window
{
    private ObservableCollection<ConnectionItem> _connectionitems = new ObservableCollection<ConnectionItem>();

    public MainWindow()
    {
        InitializeComponent();
        ConnectionItems.Add(new ConnectionItem { Name = "Item1", Ping = "150ms" });
        ConnectionItems.Add(new ConnectionItem { Name = "Item2", Ping = "122ms" });
    }

    public ObservableCollection<ConnectionItem> ConnectionItems
    {
        get { return _connectionitems; }
        set { _connectionitems = value; }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // to change a value jus find the item you want in the list and change it
        // because your ConnectionItem class implements INotifyPropertyChanged
        // ite will automaticly update the dataGrid

        // Example
        ConnectionItems[0].Ping = "new ping :)";
    }
}

public class ConnectionItem : INotifyPropertyChanged
{
    private string _name;
    private string _ping;

    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }

    public string Ping
    {
        get { return _ping; }
        set { _ping = value; NotifyPropertyChanged("Ping"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Notifies the property changed.
    /// </summary>
    /// <param name="property">The info.</param>
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

Xaml:

Xml:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
        xmlns:properties="clr-namespace:WpfApplication4.Properties"
        Title="MainWindow" Height="300" Width="400" Name="UI" >
    <Grid>
        <DataGrid Name="dataGridView" ItemsSource="{Binding ElementName=UI,Path=ConnectionItems}" Margin="0,0,0,40" />
        <Button Content="Change" Height="23" HorizontalAlignment="Left" Margin="5,0,0,12" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click" />
    </Grid>
</Window>

i added a button to show how the data updates when you change something in your list, The class ConnectionItem is where you will store all your info for the datagrid.

我添加了一个按钮来显示当您更改列表中的某些内容时数据如何更新,ConnectionItem 类是您将存储数据网格的所有信息的地方。

Hope this helps

希望这可以帮助