问题
Suppose I have a datagrid whose binded objects are of type 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
And NestedVM is defined as:
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
I've defined a datagridtextcolumn
where the cellstyle
's template has been overridden to contain a second TextBlock. I want the main text of the column to bind to the (nested) property Value
, and the second TextBlock to bind to the (nested) property SubValue
. To accomplish this an attached property NumberSource
(of type Double) has been added to GridColumnProperties. Inside the custom controltemplate, the second Textblock is binded to this NumberSource, and the NumberSource is in turn binded to 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>
However this only seems to work if NumberSource is set to a literal value; it fails when NumberSource is binded like above. Here is the current attached property definition:
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
End Class
How can I get the dependency property to correctly bind to SubValue
?
来源:https://stackoverflow.com/questions/17687305/cell-controltemplate-binding-to-attachedproperty-binding-to-property-of-view-mod