WPF iterate through items in a listview
I have a ListView
with a CheckBox
as one of the columns, bound to a boolean property of a custom object. I'm trying to figure out how to iterate through the items in the ListView
and check all the checkboxes. What I have so far is below:
XAML:
<ListView x:Name="MyListView" DockPanel.Dock="Top" Height="275" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Enabled">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsChecked}" Click="CheckBox_Click" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
VB.NET:
Private Sub SelectAll_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SelectAll.Click
Dim currSelected As Integer = MyListView.SelectedIndex
MyListView.SelectAll()
For ixItem As Integer = 0 To MyListView.SelectedItems.Count - 1
coItems(ixItem).IsChecked = True
Next
MyListView.SelectedIndex = currSelected
End Sub
I have a hunch this is actually pretty easy, and I'm just missing one line somewhere. Thanks for the help!
UPDATE: The problem more specifically is that the checkboxes that are visible when the button is pressed aren't being displayed as checked, but the ones that aren't visible (because the user needs to scroll down to see them) are displayed as checked.
UPDATE 2: As requested, here is the PropertyChanged code. I'm still fairly new to WPF, I haven't done much with INotifyPropertyChanged before.
Public Property blnIsChecked() As Boolean
Ge开发者_开发知识库t
Return _blnIsChecked
End Get
Set(ByVal value As Boolean)
_blnIsChecked = value
End Set
End Property
Public Event PropertyChangedHandler(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
You can get to the items in the listview without using SelectedItems, just use ListView.Items. that way you don't have to change the selection.
Is your underlying model not properly firing property change notifications? if not, you're setting IsChecked on them, but they aren't telling the listview.
What specificly isn't working?
精彩评论