OneWayToSource绑定不适用于UserControl DependencyProperty

时间:2014-03-18 15:41:55

标签: wpf mvvm datagrid user-controls

(使用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属性的方法。

0 个答案:

没有答案