To fix your issue with the Count of items.
First off, I took your code and removed the code behind into its own class, MainWindowViewModel.
In the constructor of the Window, I instantiated one of them and set it as the DataContext of the Window:
vbnet Code:
Class MainWindow
Public Sub New()
' This call is required by the designer.
InitializeComponent()
Me.DataContext = New MainWindowViewModel()
End Sub
End Class
Normally, you'd set the DataContext from outside the Window, but this'll do for now.
Now, our XAML looks a bit like this:
Code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Expander>
<ListView ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Text}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Expander>
<TextBlock Text="{Binding CheckedCount}" />
</StackPanel>
</Window>
(I've changed some of the property names slightly)
Now, the problem with the Binding for CheckedCount is that it needs to know when the CheckedCount property changes. Which it can't at the moment. So, get that ViewModel class implementing INotifyPropertyChanged so it can raise the PropertyChanged event when the items are checked on/off. But we don't yet know that so let's add an event to the ComboBoxItem class that lets us know when it gets checked:
vbnet Code:
Public Class ComboBoxItem
' ...
Private _IsChecked As Boolean
Public Property IsChecked() As Boolean
Get
Return _IsChecked
End Get
Set(ByVal value As Boolean)
_IsChecked = value
RaiseEvent IsCheckedChanged(Me, EventArgs.Empty)
End Set
End Property
Public Event IsCheckedChanged(sender As Object, eventArgs As EventArgs)
End Class
That lets us hook on to the event, and raise our own event in the ViewModel:
vbnet Code:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class MainWindowViewModel
Implements INotifyPropertyChanged
Private m_items As ObservableCollection(Of ComboBoxItem)
Public Sub New()
m_items = New ObservableCollection(Of ComboBoxItem)
For i As Integer = 1 To 5
Dim comboBoxItem As New ComboBoxItem() With {.Text = "Item " & i}
AddHandler comboBoxItem.IsCheckedChanged, AddressOf Me.ComboBoxItemIsCheckedChanged
m_items.Add(comboBoxItem)
Next
End Sub
Public ReadOnly Property Items As ObservableCollection(Of ComboBoxItem)
Get
Return m_items
End Get
End Property
Public ReadOnly Property CheckedCount As Integer
Get
Return m_items.Where(Function(cbi) cbi.IsChecked).Count()
End Get
End Property
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private Sub ComboBoxItemIsCheckedChanged(ByVal sender As Object, ByVal eventArgs As EventArgs)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CheckedCount"))
End Sub
End Class