WPF DataGridTemplateColumn Checkbox 元素 IsChecked 与 Converter TwoWay 绑定
我有一个 DatagridTemplate 列,其中包含一个复选框,当我的 ItemSource 上的属性为“J”时,该复选框通过转换器进行检查,而当该属性为“N”时,则取消选中该复选框。
这是可行的,但现在我希望如果我选择该复选框,则将该属性设置为“J”;如果取消选择它,则将该属性设置为“N”。
我的专栏:
<local:JNConverter x:Key="JNConverter" />
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Center">
<CheckBox Name="auto" HorizontalAlignment="center" IsChecked="{Binding Path=Autonummering, Converter={StaticResource JNConverter}, Mode=TwoWay}" />
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我的转换器:
公共类 JNConverter 实现 IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
If value IsNot Nothing AndAlso value.ToString.ToLower = "j" Then
Return True
Else
Return False
End If
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
If CType(value, Boolean) Then
Return "J"
Else
Return "N"
End If
End Function
End 类
我的 Itemsource 是一个列表(属性),属性:
Public Class Attribuut
Inherits DependencyObject
Public Shared AutonummeringProperty As DependencyProperty = DependencyProperty.Register("Autonummering", GetType(String), GetType(Attribuut))
Public Property Autonummering As String
End Class
那么我如何“反向”绑定单击复选框以将自动编号属性更改为“J”或“N”?
提前致谢
I have an DatagridTemplate Column containing an Checkbox which through an converter gets checked when a property on my ItemSource is "J" and unchecked when the property is "N".
This works, but now I want the property to be set to "J" if I select the checkbox or "N" when I deselect it.
My Column:
<local:JNConverter x:Key="JNConverter" />
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Center">
<CheckBox Name="auto" HorizontalAlignment="center" IsChecked="{Binding Path=Autonummering, Converter={StaticResource JNConverter}, Mode=TwoWay}" />
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
My Converter:
Public Class JNConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
If value IsNot Nothing AndAlso value.ToString.ToLower = "j" Then
Return True
Else
Return False
End If
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
If CType(value, Boolean) Then
Return "J"
Else
Return "N"
End If
End Function
End Class
My Itemsource is a List(Of Attribuut), Attribuut :
Public Class Attribuut
Inherits DependencyObject
Public Shared AutonummeringProperty As DependencyProperty = DependencyProperty.Register("Autonummering", GetType(String), GetType(Attribuut))
Public Property Autonummering As String
End Class
So how would I "reverse" bind the clicking on the checkbox to change the Autonummering property to "J" or "N"?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 IsChecked-Binding 上的
UpdateSourceTrigger
设置为PropertyChanged
,这样就可以了。Set the
UpdateSourceTrigger
on the IsChecked-Binding toPropertyChanged
, that should do it.