バインドされたオブジェクトが 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
のテンプレートをオーバーライドして、2 番目の TextBlock を含めるdatagridtextcolumn場所を定義しました。cellstyle列のメイン テキストを (ネストされた) プロパティValueにバインドし、2 番目の TextBlock を (ネストされた) プロパティにバインドしますSubValue。これを実現するために、NumberSource(Double 型の) 添付プロパティが GridColumnProperties に追加されました。カスタム コントロール テンプレート内で、2 番目の 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ですか?