Prithvi Venu
Prithvi Venu

Reputation: 274

Unable to access UserControl using X:Name from XAML Page

This is my user control embedded into my page list view

<ListView.ItemTemplate>
    <DataTemplate x:DataType="data:ZTask">
        <local:AddUserControl x:Name="MyUserControl" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" />
    </DataTemplate>
</ListView.ItemTemplate>

I have Named it as MyUserControl using X: Name. I tried accessing this in my page cs file but it gives me an error saying "The name 'MyUserControl' does not exist in the current context". Help me fix this issue.

Upvotes: 0

Views: 467

Answers (1)

mm8
mm8

Reputation: 169320

Which particular instance of AddUserControl are you expecting to get a reference to since there will be an AddUserControl added per item in the ListView?

If you want to do something with the AddUserControls, you could handle the Loaded event for each one of them:

<ListView.ItemTemplate>
    <DataTemplate x:DataType="data:ZTask">
        <local:AddUserControl x:Name="MyUserControl" 
                    HorizontalAlignment="Stretch" 
                    VerticalAlignment="Stretch"
                    Loaded="OnLoaded"/>
    </DataTemplate>
</ListView.ItemTemplate>

private void OnLoaded(object sender, RoutedEventArgs e)
{
    AddUserControl auc = (AddUserControl)sender;
    //...
}

Upvotes: 2

Related Questions