假设我有一个数据网格,其绑定对象的类型为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
,其中cellstyle
的模板已被覆盖以包含第二个TextBlock。我希望列的主要文本绑定到(嵌套)属性Value
,并希望第二个TextBlock绑定到(嵌套)属性SubValue
。为实现此目的,已将附加属性NumberSource
(Double类型)添加到GridColumnProperties中。在自定义controltemplate中,第二个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
?