使用C#在每行中动态添加Button到ListView

时间:2013-10-14 13:57:11

标签: c# wpf listview

我有一个ListView,我必须在其中显示从数据库表中提取的所有行。我必须在每一行中给出两个编辑和删除按钮。由于我是初学者,我在XAML中尝试了以下代码 -

<ListView Height="352" HorizontalAlignment="Left" Margin="20,90,0,0" Name="Tab1lstProductCategories" VerticalAlignment="Top" Width="1008">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Category Name" DisplayMemberBinding="{Binding Col1}" Width="200"/>
                                <GridViewColumn Header="Created Date" DisplayMemberBinding="{Binding Col2}" Width="200"/>
                                <GridViewColumn Header="Last Updated" DisplayMemberBinding="{Binding Col3}" Width="200"/>
                                <GridViewColumn Header="Edit" Width="200">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Content="Edit" Click="EditCategory" CommandParameter="{Binding Id}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Delete" Width="200">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Content="Delete" Click="DeleteCategory"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>

在C#中我写了 -

 DataTable dt=db.Select("select * from ProductCategories");
           for (int i = 0; i < dt.Rows.Count; i++)
        {
            //Button edit, delete;
            //edit = new Button();
            //edit.Content="Edit";
            //edit.Click += EditCategory;
            //delete= new Button();
            //delete.Content="Delete";
            //delete.Click += DeleteCategory;
            //edit.CommandParameter = dt.Rows[i]["Id"];
            //delete.CommandParameter = dt.Rows[i]["Id"];
            Tab1lstProductCategories.Items.Add(new { Col1 = dt.Rows[i]["CategoryName"], Col2 = dt.Rows[i]["CreatedDate"], Col3=dt.Rows[i]["LastUpdated"]});                
        }



private void EditCategory(object sender, RoutedEventArgs e)
        {
            Button b=sender as Button;
            MessageBox.Show(b.CommandParameter.ToString());
        }

    private void DeleteCategory(object sender, RoutedEventArgs e)
    {
    }

我得到了结果 - enter image description here

我想要那边的按钮,请帮帮我。

1 个答案:

答案 0 :(得分:19)

好的,所以我发现您的代码存在一些问题。

1)你真的应该创建一个像@HighCore建议的正确数据类型。看看你的代码,我认为它看起来像这样:

public class ProductCategory
{
    public int Id { get; set; }

    public string CategoryName { get; set; }

    public DateTime CreatedDate { get; set; }

    public DateTime LastUpdated { get; set; }
}

然后创建ProductCategory的集合,而不是直接添加匿名类型

DataTable dt=db.Select("select * from ProductCategories");

// Create a collection for your types
ObservableCollection<ProductCategory> list = new ObservableCollection<ProductCategory>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    ProductCategory productCategory = new ProductCategory
    {
        // Casting might be needed here depending on your data types ...
        Id = dt.Rows[i]["Id"],
        CategoryName = dt.Rows[i]["CategoryName"],
        CreatedDate = dt.Rows[i]["CreatedDate"],
        LastUpdated = dt.Rows[i]["LastUpdated"]
    };

    list.Add(productCategory);
}

2)您正在将项目直接添加到错误的ListView。您应该做的是将您的收藏设置为ItemsSource的{​​{1}}。

ListView

3)使用CellTemplate通过xaml实现Tab1lstProductCategories.ItemsSource = list; 想要的外观并将ListView绑定更改为相应的属性后,可以将DisplayMemberBinding绑定到默认值CommandParameter表达式,它将命令参数设置为它所代表的{Binding}项。

ProductCategory

通过事件处理程序 - <ListView Height="352" HorizontalAlignment="Left" Margin="20,90,0,0" Name="Tab1lstProductCategories" VerticalAlignment="Top" Width="1008"> <ListView.View> <GridView> <GridViewColumn Header="Category Name" DisplayMemberBinding="{Binding CategoryName}" Width="200"/> <GridViewColumn Header="Created Date" DisplayMemberBinding="{Binding CreatedDate}" Width="200"/> <GridViewColumn Header="Last Updated" DisplayMemberBinding="{Binding LastUpdated}" Width="200"/> <GridViewColumn Header="Edit" Width="200"> <GridViewColumn.CellTemplate> <DataTemplate> <Button Content="Edit" Click="EditCategory" CommandParameter="{Binding}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Delete" Width="200"> <GridViewColumn.CellTemplate> <DataTemplate> <Button Content="Delete" Click="DeleteCategory" CommandParameter="{Binding}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> EditCategory,您可以提取DeleteCategory

的ID
ProductCategory

这应该足以使你的代码工作,但我还有其他几点要做

一个。您应该高度考虑使用MVVM模式。这意味着不会在后面的代码中使用事件处理程序,而是使用Commands代替,并使用private void EditCategory(object sender, RoutedEventArgs e) { Button b=sender as Button; ProductCategory productCategory = b.CommandParameter as ProductCategory; MessageBox.Show(productCategory.Id); } 原来打算使用的方式。

湾考虑一些ORM框架,而不是将数据库直接绑定到您的UI。这创造了令人难以置信的紧密耦合,这将使您的代码重复性更低,更灵活。

希望这有帮助