将列表绑定到WPF中的DataGrid [英] Bind list to DataGrid in WPF

查看:69
本文介绍了将列表绑定到WPF中的DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绑定数据网格,但是只显示一行.

I am trying to bind data grid but it's only showing one row. 

        private void btn_Add_To_Cart_Click_1(object sender, RoutedEventArgs e)
        {
         Product p = new Product();
         p.Category_Name = Convert.ToString(comboCategory.SelectedValue);
         p.Product_Name = Convert.ToString(comboProduct.SelectedValue);
         p.Product_Price =Convert.ToDecimal(tb_unit_price.Text);
         p.Product_Quantity =Convert.ToInt32( tb_quantity.Text);
         list.Add(p);
         BindGrid(list);


         GrandTotal = GrandTotal + p.Product_Price;
         lbl_total_bill.Text = Convert.ToString(GrandTotal);
        }

        private void BindGrid(List<Product> list)
        {


            this.gridview.ItemsSource = list ;
            
            
        }


<DataGrid Name="gridview" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" IsReadOnly="True" Margin="497,235,10,109" ItemsSource="{Binding Product}" AutoGenerateColumns="False" FontWeight="Bold" HorizontalAlignment="Center" Width="511" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Item Category" Binding="{Binding Category_Name}" Width="100"  FontFamily="Tahoma"></DataGridTextColumn>
                <DataGridTextColumn Header="Product" Binding="{Binding Product_Name}" Width="100"  FontFamily="Tahoma"></DataGridTextColumn>
                <DataGridTextColumn Header="Quantity" Binding="{Binding Product_Quantity}" Width="60"  FontFamily="Tahoma"></DataGridTextColumn>
                <DataGridTextColumn Header="UnitPrice" Binding="{Binding Product_Price}" Width="85"  FontFamily="Tahoma"></DataGridTextColumn>
                <DataGridTemplateColumn Header="Delete" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Name="btn_delete" Content="Delete" CommandParameter="{Binding Path=UserID}"></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Update" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Update" CommandParameter="{Binding}"></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>

可以一些

推荐答案

如果您创建一个包含一项的列表,您会期望什么.  一行-一项.

Well if you create a list with one item what do you expect.  One row - one item.

哦,学习XAML,用代码做事很容易出错.

Oh and learn XAML, doing things in code is very error prone.


这篇关于将列表绑定到WPF中的DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆