Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 638
user145610 Avatar asked Jul 29 '09 08:07

user145610


2 Answers

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 !

like image 111
Jonathan ANTOINE Avatar answered Nov 15 '22 20:11

Jonathan ANTOINE


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.

like image 31
Pavel Minaev Avatar answered Nov 15 '22 20:11

Pavel Minaev