WPF DataGridTemplateColumn Checkbox 元素 IsChecked 与 Converter TwoWay 绑定

发布于 2024-11-04 02:09:25 字数 2006 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-11-11 02:09:25

将 IsChecked-Binding 上的 UpdateSourceTrigger 设置为 PropertyChanged,这样就可以了。

Set the UpdateSourceTrigger on the IsChecked-Binding to PropertyChanged, that should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文