user145610
user145610

Reputation: 3025

How to pass reference of an control to another control in XAML

I have a requirement of passing reference of control to another custom control. I created a custom control which contains a dependency property 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); }
    }

In the XAML I'm assigning Value like this

<Datagrid x:name=ClientGrid />

Here Datagrid is Microsoft WPF toolkit datagrid

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

When I try to access the value of AssociatedDatagrid property it always shows null

Can anyone tell me the right way of doing it?

Upvotes: 11

Views: 9146

Answers (2)

Jonathan ANTOINE
Jonathan ANTOINE

Reputation: 9223

Here is the code :

First element which will be referenced in the second one :

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

The second element :

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

Good luck !

Upvotes: 18

Pavel Minaev
Pavel Minaev

Reputation: 101565

You don't need Path=Name in your Binding. What you end up doing here instead is passing the value of the Name property of the DataGrid.

Upvotes: 2

Related Questions