我需要将控件的引用传递给另一个自定义控件。
我创建了一个包含依赖项属性associateDatagridProperty的自定义控件

    public static readonly DependencyProperty
        AssociatedDataGridProperty = DependencyProperty.Register(
            "AssociatedDatagrid",
            typeof(DataGrid),
            typeof(CustomControl),
            new FrameworkPropertyMetadata(null,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
            );

    public Datagrid AssociatedDatagrid
    {
        get { return (Datagrid )base.GetValue(AssociatedDataGridProperty); }
        set { base.SetValue(AssociatedDataGridProperty, value); }
    }


在XAML中,我像这样分配值

<Datagrid x:name=ClientGrid />


这里的Datagrid是Microsoft WPF工具包datagrid

<CustomControl x:Name="DatagridPaging"
               Canvas.Left="24"
               Canvas.Top="236"
               AssociatedDatagrid="{Binding ElementName=clientsGrid ,Path=Name}">


当我尝试访问AssociatedDatagrid属性的值时,它始终显示null

谁能告诉我正确的做法?

最佳答案

这是代码:

第一个元素将在第二个元素中引用:

<Label x:Name="aGivenNameLabel" Content="kikou lol"/>


第二个要素:

<ContentControl Content={Binding ElementName=aGivenNameLabel}" />


祝好运 !

07-24 09:38
查看更多