【问题标题】:Add empty row to WPF DataGrid and edit cells afterwards将空行添加到 WPF DataGrid 并随后编辑单元格
【发布时间】:2016-09-22 20:23:04
【问题描述】:

我是 WPF 新手,正在尝试将程序从 WinForms 转换为 WPF。我在尝试从 DataGridView 转换为 DataGrid 并添加行时遇到了麻烦。任何帮助表示赞赏。

原码:

for (int i = 0; i < noteArray.Count; i++)
        {
            int newIndex = dtgrdNotes.Rows.Add();
            dtgrdNotes.Rows[newIndex].Cells[0].Value = noteArray[i].ToString();
            dtgrdNotes.Rows[newIndex].Cells[1].Value = chkbx1.Checked;
            dtgrdNotes.Rows[newIndex].Cells[2].Value = chkbx2.Checked;
        }

我需要该代码才能在 WPF 中工作,并且我尝试了 dtgrdNotes.Items 甚至 DataRowView 的不同变体,但到目前为止没有任何效果。有什么建议吗?

更新* XAML 与 Richard 的控件问题相关:

<DataGrid Name="dtgrdNotes" Height="178" Width="739" HorizontalAlignment="Right" VerticalAlignment="Bottom" Canvas.Left="1" IsEnabled="False" >
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="dgtxtbxNote" Width="*" Binding="{Binding note}"/>
        <DataGridCheckBoxColumn x:Name="dgchbxInquire" Width="100" Binding="{Binding inquire}" />
        <DataGridCheckBoxColumn x:Name="dgchbxPrint" Width="100" Binding="{Binding print}" />
        <DataGridTemplateColumn x:Name="dgbtncDelete" Width="80" Header="Button">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button x:Name="dgbtncDeleteButton" Content="Delete" Width="50px" Height="10px" FontWeight="Bold" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

【问题讨论】:

    标签: c# wpf datagridview datagrid


    【解决方案1】:

    在 WPF 中,数据控件必须绑定到数据集合,通常是可观察的集合。 您可以尝试创建一个包含一个或多个空链接数据和控件的集合。 Datagrid 必须与您使用的模型相同。 我给你举个例子:

    数据网格:

    <DataGrid x:Name="dtgPersons" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Width="1*" Binding="{Binding Id}"/>
            <DataGridTextColumn Header="Name" Width="2*" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Last Name" Width="2.5*" Binding="{Binding LastName}"/>
            <DataGridTextColumn Header="Age" Width="1*" Binding="{Binding Age}"/>
            <DataGridCheckBoxColumn Width="*" Binding="{Binding Status}" Header="Status"/>
            <DataGridTemplateColumn Width="*" Header="Action">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete" Click="Button_Click"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    型号:

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public bool Status { get; set; }
    }
    

    代码:

    public MainWindow()
    {
        InitializeComponent();
    
        ObservableCollection<Person> Persons = new ObservableCollection<Person>();
    
        for(int i = 0; i < 10; i++)
        {
            Person p = new Person();
            p.Id = i;
            p.Age = i + 10;
            p.Name = "Name " + i;
            p.LastName = "LastName " + i;
            p.Status = true;
    
            Persons.Add(p);
        }
    
        dtgPersons.ItemsSource = Persons;
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var person = dtgPersons.SelectedItem as Person;
        MessageBox.Show(person.Name + " will be removed");
    }
    

    结果:

    【讨论】:

    • 那么,在那个例子中,我可以随时添加一个新行,只需创建一个新的“Person”,然后将该人添加到 Observable 集合中?
    • 是的。一个优点是,如果您编辑视图,集合会自动看到更改
    • 我不确定我是否应该为此提出一个新问题,但也许你会知道。我正在尝试实现与您的示例类似的东西,但列中的内容是控件(CheckBox、Button 等)。每次尝试时,我都会出现在单元格中的是“System.WIndows.Controls.CheckBox " 或 "System.WIndows.Controls.Button" 而不是实际的 Control。我已经正确更改了类变量类型,并且在将集合添加为 DataGrid ItemsSource 之前,它确实正确显示为我的类的一部分。在 XAML 中也为复选框正确设置了列。有什么想法吗?
    • 您需要做的就是根据您的需要更改列的类型或为每列设置一个模板。给我一个机会,我会在几分钟内更新响应代码给你一个想法。
    • 谢谢!我会用我的 Xaml 目前的样子更新我的帖子
    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多