简体   繁体   English

在DataGrid(WPF)中编辑单元格

[英]Edit Cell in DataGrid (WPF)

I am new in WPF. 我是WPF的新手。 I used to work in Winforms. 我曾经在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? 如何使用WPF中的DataGrid完成此操作? I was looking thorught stack over flow and could figure out an easy way to do this. 我一直在看待堆积如山的东西,可以想出一个简单的方法来做到这一点。

Thank you 谢谢

Ok the simplest way to handle DataGrid is by binding to an ItemSource . 好的,处理DataGrid的最简单方法是绑定到ItemSource

The example below shows how to bind your list and how changes upadte the 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: XAML:

<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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM