将集合绑定到Listview中的ComboBox

时间:2013-04-28 22:45:58

标签: wpf xaml listview collections combobox

我有一个ListView,其DataTemplat e由ComboBox和一些TextBox组成。 ComboBox绑定到映射到CollectionViewSource的Collection。 ListView可以包含任意数量的行。

问题是选择一个ComboBox中的项目会改变它们。我确实希望所有内容都填充相同的内容,但能够独立设置。

参考资料部分包含以下内容:

    <CollectionViewSource Source="{Binding ChildAccounts}" x:Key="ChildGroupedData">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="group"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

    <!-- Template for each child item in ListView -->
    <DataTemplate x:Key="ChildTemplate">
        <Grid>
            <Grid.ColumnDefinitions>                    
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="210"/>
            </Grid.ColumnDefinitions>                
            <Label Grid.Column="0" Content="Account" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
            <ComboBox Grid.Column="1" SelectedValue="{Binding Path=accFrom}" ItemsSource="{Binding Source={StaticResource ChildGroupedData}}" ItemTemplate="{StaticResource AccountTemplate}" SelectedValuePath="ID" Width="120" Style="{StaticResource RoundedComboBox}" HorizontalAlignment="Left" VerticalAlignment="Center">
                <ComboBox.GroupStyle>
                    <GroupStyle ContainerStyle="{StaticResource CustomExpanderComboGroupItemStyle}" HeaderTemplate="{StaticResource GroupHeader}"/>
                </ComboBox.GroupStyle>
            </ComboBox>
            <Label Grid.Column="2" Content="Amount" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
            <TextBox Grid.Column="3" Text="{Binding Path=amount, StringFormat='#,##0.00'}" Style="{StaticResource RoundedTextBox}" Width="80" HorizontalAlignment="Left" VerticalAlignment="Center"/>
            <Label Grid.Column="4" Content="Comment" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
            <TextBox Grid.Column="5" Text="{Binding Path=comment}" Style="{StaticResource RoundedTextBox}" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center"/>
        </Grid>
    </DataTemplate>

    <!-- ListView template -->
    <Style x:Key="ChildListViewStyle" TargetType="{x:Type ListView}">
        <Setter Property="ItemTemplate" Value="{DynamicResource ChildTemplate}"/>
        <Setter Property="Background" Value="{StaticResource CustomBackground}"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Margin" Value="10,10,10,10"/>
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="Padding" Value="0,0,50,0"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Style.Resources>
            <!-- Makes selection invisible when focus lost -->
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{StaticResource CustomBackgroundC}"/>
        </Style.Resources>
    </Style>

ListView定义为:

<ListView Grid.Column="0" x:Name="lstChildren" Margin="20,30,0,0" ItemsSource="{Binding Path=Items}" Style="{StaticResource ChildListViewStyle}"/>

编辑:

以下内容可在相关课程

中找到
Imports System.Data
Imports System.Data.OleDb
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Class ItemView
Implements INotifyPropertyChanged

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Private Sub NotifyPropertyChanged(ByVal info As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub

....

Private _ChildAccounts As ObservableCollection(Of AccountEntry)
Public Property ChildAccounts As ObservableCollection(Of AccountEntry)
    Get
        Return _ChildAccounts
    End Get
    Set(value As ObservableCollection(Of AccountEntry))
        _ChildAccounts = value
    End Set
End Property

....

Private Sub ItemView_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized

    Dim dsacmd As OleDbDataAdapter
    Dim dsa As New DataSet
    Dim dva As DataView
    Dim strSelect As String

    Try
        ' ** Open a connection to the database.        
        cn = New OleDbConnection(strConnection)
        cn.Open()

        Me.DataContext = Me

        strSelect = "SELECT Accounts.ID, Accounts.deleted, Accounts.accType, Accounts.currency as curr, IIf([Currencies.ID]=1,Accounts.comment,Accounts.comment & "" ("" & Currencies.symbol & "")"") AS comment, Currencies.comment AS currS FROM Currencies INNER JOIN Accounts ON Currencies.ID = Accounts.currency"
        dsacmd = New OleDbDataAdapter(strSelect, cn)
        dsacmd.Fill(dsa, "Accounts")
        dva = New DataView(dsa.Tables("Accounts"))
        dva.RowFilter = "accType=" & cVirtual.ToString & " AND deleted=False"
        dva.Sort = "curr, comment"
        ChildAccounts = New ObservableCollection(Of AccountEntry)(dva.ToTable.AsEnumerable().[Select](Function(i) New [AccountEntry](i("ID"), i("currS").TrimEnd(" "), i("comment"))))

....

Private Sub DisplayItem()

    ....

            strSelect = ""
            Dim Relations As Collection(Of Relation) = GetRelations(ID)
            For Each r As Relation In Relations
                strSelect &= "ID=" & r.ID.ToString & " OR "
            Next
            If strSelect <> "" Then strSelect = "SELECT * FROM Items WHERE " & strSelect.Substring(0, strSelect.Length - 4)
            If strSelect <> "" Then
                dsrcmd = New OleDbDataAdapter(strSelect, cn)
                dsr.Clear()
                dsrcmd.Fill(dsr, "Items")
                lstChildren.DataContext = dsr
            End If
....

2 个答案:

答案 0 :(得分:0)

您需要将CollectionViewSource移至DataTemplate。为了强制所有组合框项目使用相同的Source Collection,我认为你可以尝试两件事:

一个 - 使用相对来源从ChildAccounts ListView DataContext

中挑选... <Grid.Resources> <CollectionViewSource x:Key="ChildGroupedData" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.ChildAccounts}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="group" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> </Grid.Resources> ...
ObservableCollection<T>

更新:

上面的方法可以在我放在一起的 This 样本中看到。请注意,ChildAccount只是视图模型中的ListBoxItem,因此我们没有按<CollectionViewSource x:Key="ChildGroupedData" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.ChildAccounts}"> 创建新的集合,而是像您在问题中所要求的那样分享它们。

如果您的ChildAccounts属性实际上就像是在每个ListBoxItem都有一个ChildAccounts属性对象的“comment”属性,那么只需更改

<CollectionViewSource x:Key="ChildGroupedData"
                        Source="{Binding ChildAccounts}">

ListBoxItem

它应该也可以。但是,您现在每个CollectionViewSource都有一个ChildAccounts集合,并且没有共享它们的来源。

主要是在DataTemplate内定义ChildAccounts。下载附件并亲自试用并查看具体信息。

两个 - 将... <Grid.Resources> <CollectionViewSource x:Key="ChildGroupedData" Source="{x:Static local:ChildAccounts}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="group" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> </Grid.Resources> ... 作为静态变量

{{1}}

答案 1 :(得分:0)

您可以将组IsSynchronizedWithCurrentItem =“False”添加到组合框中。