Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP VisualTreeHelper.GetParent() returns null

Tags:

c#

xaml

uwp

I have a ContentDialog which has a ListView. This ListView's DataTemplate Contains a Grid and this Grid has a Button. The code goes like this:

<ContentDialog x:Name="DownloadListDialog" x:FieldModifier="public" Grid.Column="1">
    <ListView Name="AssetsListView" IsItemClickEnabled="False" Grid.Row="1" SelectionMode="Single" MaxHeight="500" ItemsSource="{x:Bind _viewModel.Assets, Mode=OneWay}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                       ...
                       ...
                    </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="viewModel:AssetViewModel">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel>
                        <TextBlock Text="{x:Bind name}"/>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{x:Bind lblFileSize}"/>
                            <TextBlock Text="{x:Bind contentSize, Mode=OneWay}"/>
                            <TextBlock Text="{x:Bind contentUrl}" Visibility="Collapsed"/>
                        </StackPanel>
                    </StackPanel>
                    <Button Content="Download" Click="Button_Click" HorizontalAlignment="Right" Grid.Column="1"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentDialog>

Here's my Button Click event handler:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var grid = VisualTreeHelper.GetParent(sender as Button) as Grid;
    ...
    ...
}

The problem is that the variable VisualTreeHelper.GetParent(sender as Button) as Grid always returns null on my PC. But this same code when I deploy on my mobile works perfectly fine (i.e, variable grid gets assigned the correct value).

UPDATE: Here's my Live Visual Tree and it confirms that the button has a parent grid. Image

App min version: build 14393 App target version: Build 15063 PC windows version: Build 17134 (version 1803)

Note: I've tried changing the App target version to 1803 but the problem remains.

like image 602
ravi kumar Avatar asked May 24 '18 10:05

ravi kumar


People also ask

Is the parent of a button null in visualtreehelper?

You could check this answer as a reference of what I stated above: FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently Nope. Even the Parent property of sender as Button is null. I can see many people have problems with getting the right controls from the VisualTreeHelper.

What is the parent of a reference in Visual Studio?

The visual whose parent is returned. The parent of the visual. reference is null. reference is not a Visual or Visual3D object. The value of reference can represent either a Visual or Visual3D object. Describes the location of the binding source relative to the position of the binding target.

What is the use of visualtreehelper?

VisualTreeHelper VisualTreeHelper VisualTreeHelper VisualTreeHelper Class. Definition. Edit. Provides utility methods that can be used to traverse object relationships (along child-object or parent-object axes) in the visual tree of your app.

What is the equivalent WinUI class to visualtreehelper?

Equivalent WinUI class: Microsoft.UI.Xaml.Media.VisualTreeHelper. Here's an example of a utility function that can copy a list of child elements of a particular type from within a visual tree.


1 Answers

As I understand from a different question there are several ways to get the parent of the VisualTreeHelper. Could it be that on your mobile or PC for that matter in the background different things are loaded so that the location of where you can find the grid object changes.

You could check this answer as a reference of what I stated above: FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently

like image 149
Niels de Schrijver Avatar answered Oct 11 '22 06:10

Niels de Schrijver