-
Nov 22nd, 2012, 06:29 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] WPF CheckedListBox - CheckedChanged Event
I've created a simple checked list box for use in my WPF application. Annoyingly, I can't quite pin down how to get my form to listen for a CheckedChanged event firing for any of the items in the list box.
My code is as follows. Firstly, a class to hold the CheckedListItems:
Code:
Imports System.ComponentModel
Public Class CheckedListItem(Of T)
Implements INotifyPropertyChanged
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _IsChecked As Boolean
Private _Item As T
Public Sub New()
End Sub
Public Sub New(item As T, Optional isChecked As Boolean = False)
Me._Item = item
Me._IsChecked = isChecked
End Sub
Public Property Item() As T
Get
Return _Item
End Get
Set(value As T)
_Item = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Item"))
End Set
End Property
Public Property IsChecked() As Boolean
Get
Return _IsChecked
End Get
Set(value As Boolean)
If Not _IsChecked = value Then
_IsChecked = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsChecked"))
End If
End Set
End Property
End Class
Code in my window:
Code:
Class ProgressWindow
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _CycleJobItems As ObservableCollection(Of CheckedListItem(Of CycleJob))
Public Property CycleJobItems() As ObservableCollection(Of CheckedListItem(Of CycleJob))
Get
Return _CycleJobItems
End Get
Set(value As ObservableCollection(Of CheckedListItem(Of CycleJob)))
_CycleJobItems = value
End Set
End Property
My XAML:
Code:
<ListBox Name="clbCycleJobs" ItemsSource="{Binding CycleJobItems}" Margin="98,76,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="167" Height="134">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Height="16" IsChecked="{Binding IsChecked}" Content="{Binding Path=Item.JobName}" IsTabStop="False" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Anyone got any ideas about the last piece of the jigsaw?
Thanks...
-
Nov 22nd, 2012, 06:49 AM
#2
Re: WPF CheckedListBox - CheckedChanged Event
-
Nov 22nd, 2012, 11:45 AM
#3
Thread Starter
Fanatic Member
Re: WPF CheckedListBox - CheckedChanged Event
Okay, I figured it out. Had to make a small tweak to the code in my form, and then add the handlers; one for the list box to add a handler to each item that's added to the list box, and one that will be used for the items themselves.
Code:
Private _CycleJobItems As ObservableCollection(Of CheckedListItem(Of CycleJob))
Public Property CycleJobItems() As ObservableCollection(Of CheckedListItem(Of CycleJob))
Get
Return _CycleJobItems
End Get
Set(value As ObservableCollection(Of CheckedListItem(Of CycleJob)))
_CycleJobItems = value
AddHandler CycleJobItems.CollectionChanged, AddressOf CycleJobItems_CollectionChanged
End Set
End Property
Private Sub CycleJobItems_CollectionChanged(sender As Object, e As Specialized.NotifyCollectionChangedEventArgs)
'# This means we're adding new items to the list.
If e.NewItems IsNot Nothing Then
For Each Item As CheckedListItem(Of CycleJob) In e.NewItems
'# Add a handler for this item so that we can listen for the checkbox element
'# being ticked or cleared.
AddHandler Item.PropertyChanged, AddressOf CheckedListItem_PropertyChanged
Next
End If
'# This means we're removing existing items from the list.
If e.OldItems IsNot Nothing Then
For Each Item As CheckedListItem(Of CycleJob) In e.OldItems
'# Remove the handler for each item.
RemoveHandler Item.PropertyChanged, AddressOf CheckedListItem_PropertyChanged
Next
End If
'# Clearing the list doesn't provide an OldItems collections, so if the action is a reset and there's
'# nothing in either collection, do whatever would happen if you removed each item individually.
If e.Action = Specialized.NotifyCollectionChangedAction.Reset AndAlso e.OldItems Is Nothing AndAlso e.NewItems Is Nothing Then
'# Do whatever
End If
End Sub
Private Sub CheckedListItem_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
'# Check that it was the IsChecked property that changed.
If e.PropertyName = "IsChecked" Then
Dim Item = TryCast(sender, CheckedListItem(Of CycleJob))
'# If the item was ticked, add it to the collection of selected jobs unless it's already there.
If Item.IsChecked Then
'# Do whatever
Else
'# Do whatever
End If
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|