1

假设我有一个数据网格,其绑定对象的类型为 ObjectVM:

  Public Class ObjectVM
    Implements INotifyPropertyChanged
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Public Property NestedObject As New NestedVM

End Class

NestedVM 定义为:

 Public Class NestedVM
    Implements INotifyPropertyChanged
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Private _SubValue As Double
    Private _Value As Double

    Public Property Value As Double
        Get
            Return _Value
        End Get
        Set(value As Double)
            _Value = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Value"))
        End Set
    End Property

    Public Property SubValue As Double
        Get
            Return _SubValue
        End Get
        Set(value As Double)
            _SubValue = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("SubValue"))
        End Set
    End Property
End Class

我已经定义了一个datagridtextcolumn' cellstyles 模板已被覆盖以包含第二个 TextBlock 的位置。我希望列的主要文本绑定到(嵌套)属性Value,第二个 TextBlock 绑定到(嵌套)属性SubValue。为此NumberSource,已将附加属性(Double 类型)添加到 GridColumnProperties。在自定义控件模板内部,第二个 Textblock 绑定到这个 NumberSource,而 NumberSource 又绑定到 SubValue:

  <DataGridTextColumn Binding="{Binding NestedObject.Value}" local:GridColumnProperties.NumberSource="{Binding NestedObject.SubValue}" >
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Template" Value="{StaticResource NumberTemplate}"/>
                    </Style>
                </DataGridTextColumn.CellStyle>

但是,这似乎仅在 NumberSource 设置为文字值时才有效;当 NumberSource 像上面那样绑定时,它会失败。这是当前的附加属性定义:

Public NotInheritable Class GridColumnProperties
Private Sub New()
End Sub
Public Shared ReadOnly NumberSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("NumberSource", GetType(Double), GetType(GridColumnProperties), New PropertyMetadata())

Public Shared Sub SetNumberSource(obj As DependencyObject, value As Double)
    obj.SetValue(NumberSourceProperty, value)
End Sub

Public Shared Function GetNumberSource(obj As DependencyObject) As Double
    Return CType(obj.GetValue(NumberSourceProperty), Double)
End Function

结束类

如何让依赖属性正确绑定到SubValue

4

0 回答 0