0
votes

I have a ListView that display a list of CheckBoxes XAML below. The Checkbox is a custom class, the code is below. The initial state of the Checkbox is set correctly. However, when the Checkbox is clicked, it should become disabled (IsEnabled = False), but this doesn't happen, despite having implemented INotify code, shown below, in the custom class (although, I suspect the error is here somewhere, or the XAML markup is wrong). I have even tried to force a binding update on the Click event, but it hasn't helped, code below. Can anyone help?

EDIT: I just realized that the explanation might be a bit disorganized. Here's what should happen: 1) the custom Checkbox is displayed with initial value (this part is working), 2) user clicks the Checkbox, 3) the Click event should fire and update the IsChecked property (it looks like this happens in the debugger when watching the properties), 4) the IsEnabled property switches to False and disables the control (this fails).

XAML:

<DataTemplate>
<CheckBox Content="test" 
    ToolTip="test"
    IsThreeState="False"
    x:Name="myCheckBox"
    Click="MyClickEvent_OnClicked"
    IsChecked="{Binding outIsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>

Custom Checkbox:

Public Class MyCustomCheckBox
    Inherits CheckBox
    Implements INotifyPropertyChanged

    Public Property outIsChecked As Boolean
        Get
            Return Me.IsChecked
        End Get
        Set(valueChecked As Boolean)
            If valueChecked <> Me.IsChecked Then
                Me.IsChecked = valueChecked
                'NotifyPropertyChanged("IsChecked")
                'OnPropertyChanged(New PropertyChangedEventArgs("IsChecked"))
                RaisePropertyChanged("IsChecked")
            End If

        End Set
    End Property

    Public Sub New()
    End Sub

    Public Sub New(ByVal inIsChecked As Boolean)
        outIsChecked = inIsChecked
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub RaisePropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

End Class

Click Event:

Public Sub MyClickEvent_OnClicked(sender As Object, e As RoutedEventArgs)

    Dim cbx1 As ChordPattern = CType(checkboxList(keyIndexes(0)), MyCustomCheckBox)
    cbx1.outIsChecked = False
    cbx1.IsEnabled = False

    'cbx1.GetBindingExpression(MyCustomCheckBox.IsEnabledProperty).UpdateTarget()
End Sub
2
I think the problem with line "Dim cbx1 As ChordPattern = CType(checkboxList(keyIndexes(0)), MyCustomCheckBox)". Try to replace it with "Dim cbx1 As MyCustomCheckBox = CType(sender, MyCustomCheckBox)"nicolas2008

2 Answers

1
votes

You may override Style and add special EventTrigger for "Click" event which will set IsEnabled property to false.

<CheckBox>
    <CheckBox.Style>
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>                       
</CheckBox>
0
votes

In your case it would be better to get rid of binding to outIsChecked property, and work directly with CheckBox.IsEnabled in click event handler.

Something like that:

Public Sub MyClickEvent_OnClicked(sender As Object, e As RoutedEventArgs)

    Dim cbx1 As ChordPattern = CType(checkboxList(keyIndexes(0)), MyCustomCheckBox)
    cbx1.IsChecked = False
    cbx1.IsEnabled = False    
End Sub

And remove the binding, because it seems to be useless here:

<DataTemplate>
<CheckBox Content="test" 
    ToolTip="test"
    IsThreeState="False"
    x:Name="myCheckBox"
    Click="MyClickEvent_OnClicked"/>
</DataTemplate>