简体   繁体   English

将两个 Listbox 中的 SelectedItems 绑定到一个 Listbox

[英]Bind SelectedItems from two Listboxes into one Listbox

How can I bind SelectedItems from two ListBoxes to one Listbox ?如何将SelectedItems从两个 ListBox 绑定到一个Listbox I'm using MVVM pattern and I set binding for SelectedItems for two ListBoxes to property in my ViewModel :我正在使用 MVVM 模式,并将两个 ListBox 的SelectedItems绑定设置为ViewModel属性:

<ListBox SelectedItemsList="{Binding EmployeeViewM.SelectedAll, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>

Then I bind SelectedAll property as an ItemsSource for my third ListBox :然后我将SelectedAll属性绑定为我的第三个ListBoxItemsSource

<ListBox ItemsSource="{Binding EmployeeViewM.SelectedAll, Mode=TwoWay}" SelectionMode="Single" >

The property on my ViewModel :我的ViewModel上的属性:

Private _selectedAll As IList = New ArrayList()

Public Property SelectedAll() As IList
    Get
        Return _selectedAll
    End Get
    Set
        _selectedAll = Value
        RaisePropertyChanged("SelectedAll")
    End Set
End Property

When I select items from my first ListBox they will appear on my third Listbox - as they should.当我从我的第一个ListBox选择项目时,它们将出现在我的第三个Listbox ——它们应该如此。 But when I select items from second ListBox as well, items selected previously from the first one disappear and I can see only items selected from the second one.但是,当我也从第二个ListBox选择项目时,之前从第一个ListBox框中选择的项目消失了,我只能看到从第二个ListBox框中选择的项目。 How can I handle third ListBox binding to get selected items from the first ListBox and from the second as well?如何处理第三个ListBox绑定以从第一个ListBox和第二个ListBox中获取所选项目?

Thank you for any suggestions谢谢你的任何建议

The simple way (but not the best) to do this is to Bind the Two ListBox to two attributes (FisrtSelectedItem and SecondSelectedItem) in The VM.执行此操作的简单方法(但不是最好的)是将两个 ListBox 绑定到 VM 中的两个属性(FisrtSelectedItem 和 SecondSelectedItem)。 In the setter of two Attributes add RaisePropertyChanged("SelectedAll").在两个属性的 setter 中添加 RaisePropertyChanged("SelectedAll")。

The SelectedAll attribute will be the union of Two Attributes SelectedAll 属性将是两个属性的并集

Public Property SelectedAll() As IList
    Get
         _selectedAll.Clear()
         _selectedAll.AddRang(_fisrtSelectedItem)
         _selectedAll.AddRang(_secondSelectedItem)        
         Return _selectedAll
    End Get
    Set
        _selectedAll = Value
        RaisePropertyChanged("SelectedAll")
    End Set
End Property

The other Way is to bind SelectionChanged Event with interactivity see how to handle WPF listbox selectionchanged event using MVVM另一种方法是将 SelectionChanged 事件与交互性绑定,看看如何使用 MVVM 处理 WPF 列表框 selectionchanged 事件

Well first off I would advise using ObservableCollection for your list of employees.首先,我建议将 ObservableCollection 用于您的员工列表。 then have a second list bound to the Third ListBox to store the Employees selected from the first two ListBoxes like so.....然后将第二个列表绑定到第三个列表框来存储从前两个列表框中选择的员工,就像这样......

    Private _SelectedItem As Employee
    Public Property SelectedItem() As Employee
        Get
            Return _SelectedItem
        End Get
        Set(ByVal value As Employee)
            _SelectedItem = value

            Dim emp As Employee = (From e In SelectedEmployees
                                   Where e.ID = value.ID Select e).FirstOrDefault()
            If emp Is Nothing Then
                SelectedEmployees.Add(value)
            End If
        End Set
    End Property

    Private _selectedAll As New ObservableCollection(Of Employee)
    Public Property SelectedAll() As ObservableCollection(Of Employee)
        Get
            Return _selectedAll
        End Get
        Set
            _selectedAll = Value
            RaisePropertyChanged("SelectedAll")
        End Set
    End Property

    Private _SelectedEmployees As New ObservableCollection(Of Employee)
    Public Property SelectedEmployees() As ObservableCollection(Of Employee)
        Get
            Return _SelectedEmployees
        End Get
        Set
            _SelectedEmployees = Value
            RaisePropertyChanged("SelectedEmployees")
        End Set
    End Property

End Class

and your XAML....和你的 XAML ....

    <StackPanel>
        <ListBox ItemsSource="{Binding SelectedAll, Mode=TwoWay}" SelectionMode="Single" SelectedItem="{Binding SelectedItem}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding RecordID}" />
                        <TextBlock Grid.Column="1" Text="{Binding ID}"/>
                        <TextBlock Grid.Column="2" Text="{Binding FName}" />
                        <TextBlock Grid.Column="3" Text="{Binding SName}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <ListBox ItemsSource="{Binding SelectedAll, Mode=TwoWay}" SelectionMode="Single" SelectedItem="{Binding SelectedItem}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding RecordID}" />
                        <TextBlock Grid.Column="1" Text="{Binding ID}"/>
                        <TextBlock Grid.Column="2" Text="{Binding FName}" />
                        <TextBlock Grid.Column="3" Text="{Binding SName}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <ListBox ItemsSource="{Binding SelectedEmployees}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding RecordID}" />
                        <TextBlock Grid.Column="1" Text="{Binding ID}"/>
                        <TextBlock Grid.Column="2" Text="{Binding FName}" />
                        <TextBlock Grid.Column="3" Text="{Binding SName}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </StackPanel>

Of course the XAML can be reduced, but for the purpose of this demo it illustrates the processes当然可以减少 XAML,但为了这个演示的目的,它说明了过程

And for completeness, here is the class that i created for the List of employees...为了完整起见,这是我为员工列表创建的类...

Public Class Employee
Private _ID As Integer

Public Property ID() As Integer
    Get
        Return _ID
    End Get
    Set(ByVal value As Integer)
        _ID = value
    End Set
End Property

Private _FName As String
Public Property FName() As String
    Get
        Return _FName
    End Get
    Set(ByVal value As String)
        _FName = value
    End Set
End Property

Private _SName As String
Public Property SName() As String
    Get
        Return _SName
    End Get
    Set(ByVal value As String)
        _SName = value
    End Set
End Property
End Class

And the constructor in the VM looked like this.... VM中的构造函数看起来像这样......

Public Sub New()
    Me.SelectedAll.Add(New Employee() With {.ID = 1, .FName = "John", .SName = "Doe"})
    Me.SelectedAll.Add(New Employee() With {.ID = 2, .FName = "Jane", .SName = "Doe"})
    Me.SelectedAll.Add(New Employee() With {.ID = 3, .FName = "Gill", .SName = "Doe"})
    Me.SelectedAll.Add(New Employee() With {.ID = 4, .FName = "Fred", .SName = "Doe"})
    Me.SelectedAll.Add(New Employee() With {.ID = 5, .FName = "Sam", .SName = "Doe"})
    Me.SelectedAll.Add(New Employee() With {.ID = 6, .FName = "Harry", .SName = "Doe"})

End Sub

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

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