2010-07-03 40 views
0

我試圖在gridview中設置組合框,但所有的組合框在它們中具有相同的值而不是來自數據庫的值。我正在使用實體框架和WPF。兩個表之間存在父子關係,但組合框的源是一個單獨的表,其中包含標籤的名稱和ID。我一直在尋找整天。希望這不會太容易解決。GridView中的組合框被同步,而不是綁定到數據庫的值

「標籤」列顯示組合框。列「標籤ID」顯示數據庫中的值。當我顯示數據TagID在不同行中的變化,但標籤列在所有行中都是相同的(第一選擇)。當我更換一個組合框時,它們都會改變。我看不到他們在哪裏迷上了。任何援助,你可以提供將不勝感激。 (澎馬?)

這裏是XAML

<Window x:Class="MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="372" Width="675" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:TagFinanceWPF"> 
<Window.Resources> 
    <CollectionViewSource x:Key="TransactionsViewSource" d:DesignSource="{d:DesignInstance my:Transaction, CreateList=True}" /> 
    <CollectionViewSource x:Key="TransactionsTransactionTagsViewSource" Source="{Binding Path=TransactionTags, Source={StaticResource TransactionsViewSource}}" /> 
    <CollectionViewSource x:Key="TagLookup" /> 
</Window.Resources> 
<Grid DataContext="{StaticResource TransactionsViewSource}"> 
    <ListView ItemsSource="{Binding Source={StaticResource TransactionsTransactionTagsViewSource}}" Margin="12" Name="TransactionTagsListView" SelectionMode="Single"> 
     <ListView.ItemContainerStyle> 
      <Style> 
       <Setter Property="Control.HorizontalContentAlignment" Value="Stretch" /> 
       <Setter Property="Control.VerticalContentAlignment" Value="Stretch" /> 
      </Style> 
     </ListView.ItemContainerStyle> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn x:Name="TransactionIDColumn1" Header="Transaction ID" Width="80"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <Label Content="{Binding Path=TransactionID}" Margin="6,-1,-6,-1" /> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
       <GridViewColumn x:Name="TagIDColumn" Header="Tag" Width="80"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox Margin="-6,-1" 
             ItemsSource="{Binding Source={StaticResource TagLookup}}" 
             DisplayMemberPath="TagName" 
             SelectedValuePath="TagID" 
             SelectedValue="{Binding TagID}" 
             IsReadOnly="True"> 
          </ComboBox> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 

       <GridViewColumn x:Name="TagIDColumn2" Header="Tag ID" Width="80"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <Label Content="{Binding Path=TagID}" /> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
      </GridView> 
     </ListView.View> 
    </ListView> 
</Grid> 

的VB代碼:

Class MainWindow 

Dim BentleyvideoEntities As TagFinanceWPF.bentleyvideoEntities = New TagFinanceWPF.bentleyvideoEntities() 

Private Function GetTransactionsQuery(ByVal BentleyvideoEntities As TagFinanceWPF.bentleyvideoEntities) As System.Data.Objects.ObjectQuery(Of TagFinanceWPF.Transaction) 

    Dim TransactionsQuery As System.Data.Objects.ObjectQuery(Of TagFinanceWPF.Transaction) = BentleyvideoEntities.Transactions 
    'Update the query to include TransactionTags data in Transactions. You can modify this code as needed. 
    TransactionsQuery = TransactionsQuery.Include("TransactionTags") 
    'Returns an ObjectQuery. 
    Return TransactionsQuery 
End Function 

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded 

    'Load data into Transactions. You can modify this code as needed. 
    Dim TransactionsViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("TransactionsViewSource"), System.Windows.Data.CollectionViewSource) 
    Dim TransactionsQuery As System.Data.Objects.ObjectQuery(Of TagFinanceWPF.Transaction) = Me.GetTransactionsQuery(BentleyvideoEntities) 
    TransactionsViewSource.Source = TransactionsQuery.Execute(System.Data.Objects.MergeOption.AppendOnly) 
    'Load data into Tags. You can modify this code as needed. 


    Dim customerList = From c In BentleyvideoEntities.Tags _ 
            Order By c.TagName 

    Dim custSource = CType(Me.FindResource("TagLookup"), CollectionViewSource) 
    custSource.Source = customerList.ToList() 

End Sub 

末級

回答

1

我發現this,同時研究您的問題,它聽起來像你遇到的完全相同的問題。

SNIPPIT從鏈接:

我不知道這是否會幫助,但我 在讀克里斯塞爾斯「的Windows 形式在C#中,腳註, 頁482綁定」,即數據源綁定到每個組合框的 ,並由一個 公共綁定管理器管理,該管理器又是 是綁定上下文的一部分。 綁定amager將所有組合框 保持同步到 數據庫中的同一行。但是,如果每個組合框 具有不同的綁定上下文,因此 不同的綁定管理器,則組合框可以顯示來自同一數據源的不同行 。

基於this second文章(和建議的解決方案),你將需要使用該行的數據綁定事件設置組合框的結合,使每一行綁定創建綁定管理的新instace。

+0

我沒有機會測試它,因爲我已經轉移到另一個項目,但它看起來正是我所期待的。謝謝你的幫助。 – 2010-07-28 18:38:15