Search code examples
c#wpfxamlcommandparameter

How to find an element by name in other user control?


I have a user control (MainView) with an other user control (GridView):

<local:MyGridView x:Name="GridView"/>

In my MainView, I have a button:

<Button Command="{Binding TestCommand}"  
        CommandParameter="{Binding ElementName=DataGrid}" 
        Content="Test"/>

In my GridView, I have a data grid that I want to send as a CommanParameter. If the GridView code was in the MainView code, this syntax would work. What should I change?

I tried {Binding ElementName=GridView.DataGrid} - no success.


Solution

  • Very simple.

    <local:MyGridView x:Name="**GridView**"/>
    
    <Button Command="{Binding TestCommand}"  
            CommandParameter="{Binding ElementName=**GridView**}" 
            Content="Test"/>
    

    You should change the ElementName from DataGrid To GridView.