【问题标题】:Create a DataGrid in WPF with column type equal with DataGridViewButtonColumn of WinForm在 WPF 中创建一个 DataGrid,其列类型等于 WinForm 的 DataGridViewButtonColumn
【发布时间】:2015-06-20 14:47:32
【问题描述】:

在 WinForm 中,我可以通过循环在 DataGridView 列上以编程方式添加行,可以这样说

private void rowsAdder()
{
    for(int u = 0; u<= 10; u++)
    {
         classSelect.Rows.Add("Number " + u);
         //classSelect is a name of a DataGridView, where the first column is a DataGridViewButtonColumn
    }
}

那么我的问题是:

-WPF 中在 DataGrid 中添加行的方法有哪些(使用循环的 Rows.Add() 等)?

-如何将列类型设置为ButtonColumn?

如果有帮助,我正在使用 .NET 4.5

【问题讨论】:

  • @GrantWinney 你能给我一个示例代码吗?

标签: c# wpf datagridview wpfdatagrid


【解决方案1】:

这是一个非常简单的例子:

<Window x:Class="DataGridUpdateSourceTriggerOneWay.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DataGridUpdateSourceTriggerOneWay"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid x:Name="DataGrid1" 
              IsReadOnly="False"
              AutoGenerateColumns="False"
              CanUserAddRows="False"
              ItemsSource="{Binding data}">

        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Field">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Path=Name, Mode=TwoWay, 
                    UpdateSourceTrigger=PropertyChanged}" Width="Auto"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Length of Field">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Path=Length, Mode=TwoWay, 
                    UpdateSourceTrigger=PropertyChanged}" Width="Auto"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

这是您的 DataGrid,它的 ItemsSource 绑定到下面代码隐藏中存在的数据:

public partial class MainWindow : Window
{
    public ObservableCollection<Data> data { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        data = new ObservableCollection<Data>();
        data.Add(new Data() { Name = "Data1", Length = 1 });
        data.Add(new Data() { Name = "Data2", Length = 2 });
        this.DataContext = this;
    }
 }

集合需要通知其绑定视图元素其内容已更改(添加或删除的项目),这是ObservableCollection&lt;Data&gt; 的工作。

这里是数据类:

public class Data : INotifyPropertyChanged
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    private int _Length;

    public int Length
    {
        get { return _Length; }
        set
        {
            _Length = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Length"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

INotifyPropertyChanged 接口是 Observer 模式实现的一部分,用于特定目的:通知订阅者发布者的属性值刚刚更改。因此,在现有集合的实例上更改这两个属性、名称或长度将导致视图更新。

如果您需要更多详细信息,请告诉我。

哦,我忘了,如何添加新行?只需为放置在 View 上某处的 Button 处理 Click 事件,然后将新的 Data 实例添加到数据集合中。

例如:

data.Add(new Data() { Name = "Data3", Length = 3 });

【讨论】:

  • 我好像有点问题,INotifyPropertyChangedObservableCollection&lt;&gt; 不会出现,VS 说找不到,是不是漏了什么?
  • 对不起,我不知道我应该包括System.ComponentModel 现在已经完成了。
  • @TommyAriaPradana 啊,我刚上班.. 很抱歉我没有提供更多详细信息.. 但我很高兴听到你说对了。
  • 没关系,你的回答对我很有帮助,但还有一个问题,我已经用ObservableCollection&lt;&gt; 绑定了我的DataGrid,我已经初始化了它,但即使在我添加了项目之后,项目不会反映在 DataGrid 中
  • @TommyAriaPradana 您可以运行我的示例,看看那里发生了什么。将您的集合添加为属性: public ObservableCollection data { get;放;并像我一样初始化它。它应该工作。如果没有,请发布更新,我们可以一起解决。
猜你喜欢
  • 2017-07-15
  • 2011-12-21
  • 1970-01-01
  • 2013-10-22
  • 2011-02-25
  • 1970-01-01
  • 2013-06-27
  • 2020-10-16
  • 1970-01-01
相关资源
最近更新 更多