【问题标题】:Comboboxes in GridView are synchronized instead of bound to the value from the databaseGridView 中的组合框是同步的,而不是绑定到数据库中的值
【发布时间】:2011-03-11 08:11:57
【问题描述】:

我尝试在 gridview 中设置组合框,但所有组合框的值都相同,而不是数据库中的值。我正在使用实体框架和 WPF。两个表之间存在父子关系,但组合框的来源是一个单独的表,其中包含标签的名称和 ID。我整天都在找。希望这不会太容易解决。

“标签”列显示组合框。 “标签 ID”列显示数据库中的值。当我显示数据时,不同行中的 TagID 会发生变化,但所有行中的 Tag 列都是相同的(第一选择)。当我更改一个组合框时,它们都会更改。我看不出他们是在什么地方连在一起的。您可以提供的任何帮助将不胜感激。 (布勒?)

这里是 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

结束类

【问题讨论】:

    标签: vb.net entity-framework combobox binding


    【解决方案1】:

    我在研究您的问题时发现了this,这听起来与您遇到的问题完全相同。

    链接摘录:

    我不确定这是否会有所帮助,但我 正在阅读 Chris Sells 'Windows C# 中的表单绑定、脚注、页面 482',数据源绑定到 每个组合框都由一个 通用的绑定管理器,反过来 是绑定上下文的一部分。这 绑定 aager 保留所有组合框 同步到同一行 数据库。但是,如果每个组合框 具有不同的绑定上下文,因此 一个不同的绑定管理器,然后 组合框可以显示不同的行 来自同一数据源。

    根据this second 文章(和建议的解决方案),您需要使用行数据绑定事件来设置组合框的绑定,以便为每个行绑定创建一个新的绑定管理器实例。

    【讨论】:

    • 我没有机会测试它,因为我已经转移到另一个项目,但它看起来正是我想要的。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多