2012-12-17 85 views
1

我是WPF中的新手。我曾经在Winforms中工作。在DataGrid中编辑单元格(WPF)

在Winforms中我有DataGridView,当我想要单元格值时,我可以更改它。

只需用:

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

它的工作原理。

如何使用WPF中的DataGrid完成此操作? 我正在寻找充满流量的堆栈,并可能找出一个简单的方法来做到这一点。

谢谢

+2

我从来没有碰过DadaGrid从后面的代码,有一个原因,你不只是改变了的ItemSource价值? –

+0

我该怎么做? 我没有来自数据库的信息。我只是想根据功能更新一些内容。想象一下,一个表格显示了基于ping的计算机有什么连接。我想在运行时更改单元格的值,但我没有数据库。 –

+1

你不需要数据库,只是一个列表或其他东西,我会尝试为你创建一个简单的例子,坚持:) –

回答

1

好处理DataGrid最简单的方法是通过结合一个ItemSource

下面的示例显示了如何绑定您的列表以及如何更改数据网格。

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:

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

我增加了一个按钮来说明如何将数据更新时,您在您的列表改变的东西,类ConnectionItem这里,您将存储在DataGrid您的所有信息。

希望这有助于

+0

感谢这帮了我很多。现在我有它的工作。 更多的问题,我看到DataGrid显示您的整个集合,与您的对象的所有propertys。如果我需要某些专栏仅在某些时刻显示,我该怎么办? –

+0

这取决于你想如何以及何时鞋/藏它们,这里有一些很好的文章: http://stackoverflow.com/questions/6857780/how-to-hide-wpf-datagrid-columns-depending-on -a-property http://www.c-sharpcorner.com/uploadfile/dpatra/hideun-hide-columns-using-context-menu-in-datagrid-in-wpf/ 或者在代码背后,它很容易' dataGridView.Columns.FirstOrDefault(c => c.Header.ToString()==“Ping”)。Visibility = Visibility.Hidden; –