(使用VS 2010)
我已将UserControl
定义为Curves
集合作为依赖属性:
Public Class GroupedChartSetupControl
....
Public Shared ReadOnly CurvesProperty As DependencyProperty =
DependencyProperty.Register("Curves", GetType(ObservableCollection(Of CurveVM)), GetType(GroupedChartSetupControl), New UIPropertyMetadata(Nothing))
Public Property Curves As ObservableCollection(Of CurveVM)
Get
Return CType(GetValue(CurvesProperty), ObservableCollection(Of CurveVM))
End Get
Set(value As ObservableCollection(Of CurveVM))
SetValue(CurvesProperty, value)
End Set
End Property
然后在控件的构造函数中,我初始化Curve集合:
Sub New()
InitializeComponent()
SetValue(CurvesProperty, New ObservableCollection(Of CurveVM))
....
接下来,在Window
我将此UserControl
放入datagrid
的行模板中。我想将Curves
集合绑定到SubCurves
的{{1}}中的源属性datagrid
:
viewmodel
请注意绑定的 <DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Grid>
<local:GroupedChartSetupControl Curves="{Binding SubCurves, Mode=OneWayToSource}"
....
。 OneWaytoSource
应该将UserControl
集合发送到行的Curves
,而不是相反。但是当我调试时,ViewModel
viewmodel
属性始终为null。我的WPF知识相当基础,但我认为依赖属性是暴露SubCurves
属性的方法。