Wpf 如何在XAML中将一个控件的引用传递给另一个控件
我需要将控件的引用传递给另一个自定义控件。 我创建了一个自定义控件,其中包含一个依赖项属性Wpf 如何在XAML中将一个控件的引用传递给另一个控件,wpf,data-binding,controls,Wpf,Data Binding,Controls,我需要将控件的引用传递给另一个自定义控件。 我创建了一个自定义控件,其中包含一个依赖项属性AssociatedDataGridProperty public static readonly DependencyProperty AssociatedDataGridProperty = DependencyProperty.Register( "AssociatedDatagrid", typeof(DataGrid),
AssociatedDataGridProperty
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 toolkit Datagrid
<CustomControl x:Name="DatagridPaging"
Canvas.Left="24"
Canvas.Top="236"
AssociatedDatagrid="{Binding ElementName=clientsGrid ,Path=Name}">
当我尝试访问AssociatedDatagrid属性的值时,它总是显示null
谁能告诉我做这件事的正确方法吗 在
绑定中不需要Path=Name
。在这里,您要做的是传递DataGrid
的Name
属性的值 以下是代码:
将在第二个元素中引用的第一个元素:
<Label x:Name="aGivenNameLabel" Content="kikou lol"/>
第二个要素:
<ContentControl Content={Binding ElementName=aGivenNameLabel}" />
我不会再发布另一个答案(因为我同意),我只会在这里添加一个注释,以防仍然有混淆。在幕后,绑定主要尝试执行以下操作:AssociatedDatagrid=(clientsGrid.Name作为Datagrid);结果将为空。