简体   繁体   English

在WPF中对DataGrid进行TwoWay绑定

[英]TwoWay binding on a DataGrid in WPF

I have a WPF DataGrid that I am filling using 我有一个我正在使用的WPF DataGrid

var allLines = from Lines in ctx.InvoiceLines join PerPs in ctx.ProductsViews on Lines.ProductCode equals PerPs.ProductCode 
               where (Lines.BranchNo == BrNo) && (Lines.Docket == Docket)
               select new { Lines.ProductCode, Lines.Description, Lines.Inv_Quantity, Lines.Grn_Quantity,
                Lines.Inv_Price,Lines.Grn_Price,Lines.Inv_Total, Lines.Grn_Total, Lines.AnalCode,
                Lines.Vat_Rate, Lines.GrnNo,Lines.Comment , PerPs.OuterUnits};

dgGrid.ItemsSource = allLines;

I want to use two way binding to update the database when any of the values are changed or when a new row is added. 我希望在更改任何值或添加新行时使用双向绑定来更新数据库。 Is that possible? 那可能吗?

My xaml code is 我的xaml代码是

<DataGrid Grid.Row="3" x:Name="dgGrid" DataContext="{Binding}" FontSize="16" HorizontalScrollBarVisibility="Visible" 
              VerticalScrollBarVisibility="Visible" SelectionUnit="FullRow" SelectionMode="Extended" AutoGenerateColumns="False"
              SelectionChanged="dgGrid_SelectionChanged" >
        <DataGrid.Columns>
            <DataGridTextColumn Width="Auto" Header="ProductCode"  Binding="{Binding ProductCode, Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="250" Header="Description"  Binding="{Binding Description,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" FontSize="14"/>
            <DataGridTextColumn Width="61" Header="Inv_Quantity" Binding="{Binding Inv_Quantity,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="63" Header="Grn_Quantity" Binding="{Binding Grn_Quantity,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="59" Header="Inv_Price" Binding="{Binding Inv_Price,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="61" Header="Ord_Price" Binding="{Binding Grn_Price,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="72" Header="Inv_Total" Binding="{Binding Inv_Total, Converter={StaticResource Currency},  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="74" Header="Grn_Total" Binding="{Binding Grn_Total, Converter={StaticResource Currency},  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="58" Header="AnalCode" Binding="{Binding AnalCode,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="40" Header="Vat_Rate" Binding="{Binding Vat_Rate,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="Auto" Header="GrnNo"  Binding="{Binding GrnNo,  Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="Auto" Header="Comment" Binding="{Binding Comment, Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Width="Auto" Header="PerP" Binding="{Binding OuterUnits}" IsReadOnly="True"/>
        </DataGrid.Columns>
        <DataGrid.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightSteelBlue"/>
        </DataGrid.Resources>
    </DataGrid>

When I run this code all but the PerP column is empty. 当我运行此代码时,除了PerP列之外的所有代码都是空的。

I'm quite lost as to how to do the best way so any help will be very much appreciated. 我很失落如何做到最好的方式,所以任何帮助将非常感激。

All the Bindings for your DataGridTextColumn besides the "PerP" column are specifying the path twice. 除“PerP”列之外, DataGridTextColumn所有Bindings都指定了两次路径。 For example: 例如:

<DataGridTextColumn Width="Auto" Header="ProductCode"  Binding="{Binding ProductCode, Mode=TwoWay, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>

Here your Binding is first specifying the path to be "ProductCode" and then also specifying it to be "IsSelected". 这里你的Binding首先指定路径为“ProductCode”,然后将其指定为“IsSelected”。 Since there is no "IsSelected" property on the objects in the collection that you are binding to the grid, if you run this under the debugger you should see binding errors in the Output window. 由于集合中的对象没有“IsSelected”属性,因此绑定到网格时,如果在调试器下运行此属性,则应在“输出”窗口中看到绑定错误。 If you remove the Path=IsSelected for those bindings then the column values should be bound correctly. 如果为这些绑定删除Path=IsSelected ,则应正确绑定列值。

Your datasource is a collection of an anonymous type so the var is necessary, but as others have said two-way binding to that collection isn't going to work for updates back to the source. 您的数据源是匿名类型的集合,因此var是必需的,但正如其他人所说,对该集合的双向绑定不适用于更新回源。

If the "allLines" variable is a DataTable , you could just set your DataGrid's AutoGenerateColumns to true. 如果“allLines”变量是DataTable ,则可以将DataGrid的AutoGenerateColumns设置为true。
You also need not to use the "DataContext" property, but "ItemsSource" instead. 您还不需要使用“DataContext”属性,而是使用“ItemsSource”。
Something like this: 像这样的东西:

<DataGrid ItemsSource="{Binding Path=allLines.DefaultView}" ... />

To update back the database on changes, call the Update() method of your datatable, on a "Save" button click for example. 要在更改时更新数据库,请调用数据表的Update()方法,然后单击“保存”按钮单击。

If "allLines" is not a DataTable, then remove this hellish "var" keyword and tell us the true nature of the variable =) 如果“allLines”不是DataTable,那么删除这个地狱般的“var”关键字并告诉我们变量的真实性质=)

Your allLines variable is an enumerable of an anonymous type. 您的allLines变量是匿名类型的可枚举。 In C# anonymous types are read-only - the properties cannot be edited, and the changes can't be saved. 在C#中,匿名类型是只读的 - 无法编辑属性,并且无法保存更改。

Try making your query: 尝试进行查询:

var allLines = 
    from Lines in ctx.InvoiceLines 
    join PerPs in ctx.ProductsViews on Lines.ProductCode equals PerPs.ProductCode 
    where (Lines.BranchNo == BrNo) && (Lines.Docket == Docket)
    select Lines;

dgGrid.ItemsSource = allLines.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM