开发者

Binding to a List<>

开发者 https://www.devze.com 2022-12-27 21:25 出处:网络
I have a datagrid like this: <dg:DataGrid Name=\"dg\" AutoGenerateColumns=\"False\" CanUserDeleteRows=\"True\">

I have a datagrid like this:

<dg:DataGrid Name="dg" AutoGenerateColumns="False" CanUserDeleteRows="True">
                    <dg:DataGrid.Columns>
                        <dg:DataGridTextColumn Header="Product Code" x:Name="columnProductCode" Binding="{Binding Path=Product.ProductCode}" IsReadOnly="True" ></dg:DataGridTextColumn>
                        <dg:DataGridTextColumn Header="Product Name" x:Name="columnProductName" Binding="{Binding Path=Product.Name}" IsReadOnly="True" ></dg:DataGridTextColumn>
                        <dg:DataGridTextColumn Header="ProductMeasure" x:Name="columnDonViTinh" Binding="{Binding Path=Product.Measure IsReadOnly="True"></dg:DataGridTextColumn>
                        <dg:DataGridTextColumn Header="Quantity" x:Name="ColumnQuantity" Binding="{Binding Path=Quantity IsReadOnly="False"></dg:DataGridTextColumn>
                    </dg:DataGrid.Columns>
</dg:DataGrid>

In the behind code, I have a struct like this:

private struct ProductDetail
  开发者_如何学Python      {

            public TProduct Product { get; set ; } // TProduct is a class provied by a web service
            public int Quantity { get; set; }
        }

and a List like this:

        private IList<ProductDetail> bs = new List<ProductDetail>();

I had tried to fill data to "bs". And binding like this:

this.dg.ItemsSource = this.bs;

Everything is ok. I can insert a new row, delete row, but when I try to modified the column Quantity then click on the header of the datagrid (to resort) --> the Quantity column change to it is before.

How can I fix this problem. Thanks advanced.


On your datagrid, you must specify that the data changes when you change the property, rather than leave the cell. To do that, you add the UpdateSourceTrigger=PropertyChanged to your binding and then it should work. Your DataGrid will now look like this:

<dg:DataGrid Name="dg" AutoGenerateColumns="False" CanUserDeleteRows="True">
    <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Header="Product Code"
            x:Name="columnProductCode" 
            Binding="{Binding Path=Product.ProductCode, UpdateSourceTrigger=PropertyChanged}"
            IsReadOnly="True" >
        </dg:DataGridTextColumn>
        <dg:DataGridTextColumn Header="Product Name"
            x:Name="columnProductName"
            Binding="{Binding Path=Product.Name, UpdateSourceTrigger=PropertyChanged}"
            IsReadOnly="True" >
        </dg:DataGridTextColumn>
        <dg:DataGridTextColumn Header="ProductMeasure"
            x:Name="columnDonViTinh"
            Binding="{Binding Path=Product.Measure, UpdateSourceTrigger=PropertyChanged}"
            IsReadOnly="True">
        </dg:DataGridTextColumn>
        <dg:DataGridTextColumn Header="Quantity"
            x:Name="ColumnQuantity"
            Binding="{Binding Path=Quantity, UpdateSourceTrigger=PropertyChanged}" 
            IsReadOnly="False">
        </dg:DataGridTextColumn>
    </dg:DataGrid.Columns>
</dg:DataGrid>


I don't know the full API of the DataGrid but I think you have to commit changes before they are pushed back to the underlying object. Looking at the docs It looks like the edit isn't committed until you change cells or press enter. If you sort before doing either of those actions, the edit might be cancelled.


OK, I have try to define a class (with TProduct and Quantity attribute also) instead of the current struct. And .. it works =.=

private class ProductDetail
{
    public TProduct Product { get; set ; } // TProduct is a class provied by a web service
    public int Quantity { get; set; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号