问题
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
回答1:
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>
回答2:
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>
来源:https://stackoverflow.com/questions/34242718/update-wpf-checkbox-in-listview-from-enabled-to-disabled-when-clicked-inotify-n