How to find the right item out of a Listbox bound

2019-08-03 22:11发布

问题:

i have a ListBox with its ItemsSource bound to a CollectionViewSource, which is bound to an ObservableCollection.

The template for the ListBoxItem includes a CheckBox which, when checked, indicates that the item is selected.

My Problem is, that I have no idea how to find out which items have their CheckBox clicked.

回答1:

What are the checkboxes bound to? If it is bound to a property on the objects in your collection, then you shouldn't need to figure out which Checkbox was clicked. If it is not bound to something on the object or ViewModel you might be able to get the SelectedItem from the listbox.

Previously I have bound the SelectedItem property of the listBox to a property on my ViewModel so that I can have things that run whenever it changes.

As to getting the index, you should be able to match the idex returned from the listbox with an index of an item in the CollectionViewSource.View which contains the current view of the collection in the order it is displayed.

If you are not using MVVM, I would suggest it. I started out not using it and quickly got mired in the code-behind.

Example in MVVM

Lets say we have MyClass with three string properties and a boolean. In MVVM, we have a MyClassViewModel which has a property to contain an instance of MyClass along with any needed functionality for the View (a listboxitem in this case). We also have a MyWindowViewModel which will hold the collection of data, and other stuff for our main view.

<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>

Example ViewModels

Public Class MainViewModel
    Inherits ViewModelBase

    Public Property MyClassCollection as New ObservableCollection(Of MyClassViewModel)

End Class

Public Class MyClassViewModel
    Inherits ViewModelBase

    Public Property ModelClass as MyClass

    Public Sub New()
    End Sub
    Public Sub New(ByRef CustClass as MyClass)
        ModelClass = CustClass
    End Sub

End Class

When we get our data, we put it in the ObservableCollection(Of MyClassViewModel). I usually do this in the WorkCompleted handler for a data retrieval backgroundworker.

   For Each mc as MyClass in e.Results
      MyClassCollection.Add(New MyClassViewModel(mc)
   Next

The listbox will still get it's items from the observable collection through the collectionViewSource, but now they will be of type MyClassViewModel.

<DataTemplate DataType="{x:Type local:MyClassViewModel}">
    <Border BorderBrush="#FF036200" BorderThickness="1" Background="#FF3CC600" CornerRadius="10">
        <Grid Height="Auto" Margin="4" DataContext={Binding ModelClass}>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.5*"/>
                <ColumnDefinition Width="0.5*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.5*"/>
                <RowDefinition Height="0.5*"/>
            </Grid.RowDefinitions>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding StringProp1}" VerticalAlignment="Top" Margin="0" FontSize="16"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding StringProp2}" VerticalAlignment="Top" Margin="0" Grid.Row="1" FontSize="16"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding StringProp3}" VerticalAlignment="Top" Grid.Column="1" Margin="0" FontSize="16"/>
            <CheckBox Content="Is Bool True?" HorizontalAlignment="Left" IsChecked="{Binding BoolProp}" VerticalAlignment="Top" Grid.Column="1" Margin="0" Grid.Row="1" FontSize="16"/>
        </Grid>
    </Border>
</DataTemplate>

Thus when someone clicks the checkbox, it changes the value on the object underneath, and since the listbox item represents the ViewModel, if you want to change something on the ListBoxItem, databind it to a property on the ViewModel and change that property.

For instance, lets say you want to change the color of the ListBoxItem to a random color when a user checks the checkbox (and for whatever reason you don't want to use a trigger or something. You could create a property on the MyClassViewModel of type Brush and Databind the Border.Background to it, and a property of Boolean which sets the MyClass property to the same value. In the setter for the boolean property, you check the value, and if it is true, set the brush value (Random Brush generator not included).

This way the ViewModel tells the view how to display the data in the Model, and can intercept datachanges from the View and do something with it if necessary.

<DataTemplate DataType="{x:Type local:MyClassViewModel}">
        <Border BorderBrush="#FF036200" BorderThickness="1" Background="{Binding BorderBackground}" CornerRadius="10">

Reply to Comment

Everyone kind of has their own way of doing MVVM. I have several different kinds of ViewModels that I use. Some are really Form View Models (Used to control the way a form works), Model View Models (used to tell the View [generally a usercontrol for editing details or a ItemsControl DataTemplate] how to display the data). With Form View Models, I sometimes break them down to NavigationViewModel and Record Maintenance ViewModels depending on the situation.

In this case I really have a ViewModel for controlling the form and a Viewmodel for displaying the data. The Form View Model often handles button commands for adding or removing items in a collection, or specifying logic that tells the View whether the save or other action button is enabled.

INotifyPropertyChanged

Very light ViewModelBase class implementing INPC

Imports System.ComponentModel

Public MustInherit Class ViewModelBase
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub OnPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

End Class

And in the ViewModels that inherit:

Public Property IsSelected() As Boolean
    Get
        Return m_IsSelected
    End Get
    Set(ByVal value As Boolean)
        m_IsSelected = value
        OnPropertyChanged("IsSelected")
    End Set
End Property