将SelectedItems从两个列表框绑定到一个列表框

时间:2016-05-11 09:20:22

标签: wpf vb.net xaml mvvm listbox

如何将SelectedItems从两个ListBox绑定到一个Listbox? 我正在使用MVVM模式,并为我SelectedItems中的两个ListBoxes设置ViewModel的绑定:

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

然后我将SelectedAll属性作为ItemsSource绑定到我的第三个ListBox

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

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

当我从第一个ListBox中选择项目时,它们将显示在我的第三个Listbox上 - 正如他们应该的那样。但是当我从第二个ListBox中选择项目时,之前从第一个项目中选择的项目会消失,我只能看到从第二个项目中选择的项目。如何处理第三个ListBox绑定以从第一个ListBox和第二个boolean isExpanded获取所选项目?

感谢您提出任何建议

2 个答案:

答案 0 :(得分:-1)

这样做的简单方法(但不是最好的方法)是将两个ListBox绑定到VM中的两个属性(FisrtSelectedItem和SecondSelectedItem)。在两个属性的setter中添加RaisePropertyChanged(&#34; SelectedAll&#34;)。

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

另一种方法是将SelectionChanged事件与交互性绑定,请参阅how to handle WPF listbox selectionchanged event using MVVM

答案 1 :(得分:-1)

首先,我建议您使用ObservableCollection作为员工列表。然后有第二个列表绑定到第三个ListBox来存储从前两个ListBox中选择的Employees,如此.....

    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

和你的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>

当然可以减少XAML,但是为了演示的目的,它说明了过程

为了完整起见,这是我为员工名单创建的课程......

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

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