2011-10-21 31 views
3

上一個DataGrid結合我有我使用雙向在WPF

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; 

我想用雙向綁定更新數據庫時的任何值被改變或者新的行填充一個WPF的DataGrid被添加。那可能嗎?

我的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> 

當我運行這段代碼所有,但PERP欄是空的。

我很迷茫,如何做到最好的方式,所以任何幫助將非常感激。

回答

3

所有BindingsDataGridTextColumn除了「PERP」列中兩次指定的路徑。例如:

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

這裏您Binding首先指定路徑是「產品代碼」,然後同時指定它是「IsSelected」。由於集合中綁定到網格的對象中沒有「IsSelected」屬性,因此如果在調試器下運行該屬性,則應該在「輸出」窗口中看到綁定錯誤。如果刪除那些綁定的Path=IsSelected,則應該正確綁定列值。

您的數據源是匿名類型的集合,因此var是必需的,但是正如其他人所說的,對該集合的雙向綁定不適用於更新回源。

+0

是否有另一種方法可以支持更新的雙向綁定? – Noelle

+1

是的,通過綁定到你想要更新的實際對象的實例,在你的情況下這將是由實體框架跟蹤的實體。我假設你最終想要將更改保存回數據庫,因此更新會影響正在進行更改跟蹤的EF實體,這樣以後所有更改都可以刷新到數據庫。 –

+0

你有鏈接到任何網站,可以顯示如何做到這一點? – Noelle

2

如果「allLines」變量是DataTable,那麼可以將DataGrid的AutoGenerateColumns設置爲true。
您也不需要使用「DataContext」屬性,而是使用「ItemsSource」。
事情是這樣的:

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

要在更改更新回數據庫,調用一個「保存」按鈕,點擊比如你的DataTable的Update()方法。

如果「allLines」不是一個DataTable,然後刪除這個地獄般的「變種」關鍵字,告訴我們變量的本色=)

+0

當我使用ItemsSource DG是空的。我不知道什麼數據類型allLines實際上是 – Noelle

+1

TBH它從代碼中很清楚地看到,賦值左側的變量不是一個DataTable,因此不相關。這顯然是一個匿名類型的集合,所以var是必需的。 –

2

allLines變量是一個匿名類型的枚舉。在C#中匿名類型是隻讀的 - 屬性不能被編輯,並且更改不能被保存。

請嘗試將查詢:

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(); 
+0

如何使其可讀(不是匿名)? – Noelle

+1

除了將查詢結果投影到匿名類型的集合(即使用'new {...}')之外,因爲Paul應該跟蹤對它們所做的更改,所以請選擇上面顯示的實際行。 –