15

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.

ravi kumar
  • 1,548
  • 1
  • 13
  • 47
  • 1
    Do you want a workaround or to know why it's null? Workarounds are doable, answers are more difficult – Johnny Westlake May 24 '18 at 12:58
  • A proper answer would be great. But i would also like to add that the same code used to work fine a while back. – ravi kumar May 24 '18 at 13:37
  • Weird - I've tried your code and seems to work fine. Can you check if `var obj = VisualTreeHelper.GetParent(sender as Button);` returns anything? – Romasz May 26 '18 at 20:02
  • Use the tools that are available to you. Go to **Debug/Windows/Live Visual Tree** and verify it. [Here's your list view containing 5 items, showing the first item with the button as a child of the grid, as expected](https://imgur.com/4Hicfy6). – jsanalytics May 26 '18 at 21:10
  • @Romasz that returns null. – ravi kumar May 27 '18 at 06:29
  • @jsanalytics I've verified it it returns null on pc and correct value on mobile. – ravi kumar May 27 '18 at 06:31
  • @ravikumar are you saying you verified that the visual tree is the same in both cases, the one that works and the one that doesn't? – jsanalytics May 27 '18 at 14:13
  • Also please update your question showing the exact context your `ContentDialog` is being called. Your **OS Edition, Version and Build** should be pertinent information as well. – jsanalytics May 27 '18 at 14:17
  • @jsanalytics I've updated my question. – ravi kumar May 28 '18 at 16:46
  • I tried to create a sample to test this issue, but after the dialog shows, I clicked the button, I can get its parent Grid. I can not reproduce this issue in my side, it would help us look into this issue in the same point if you can provide a reproducible project. Moreover, I found you have asked this issue in the MSDN, please check the [thread](https://social.msdn.microsoft.com/Forums/en-US/14c7ed67-2902-4515-a2c8-2a4e9bdf3343/uwp-visualtreehelpergetparent-returns-null?forum=wpdevelop). – Breeze Liu - MSFT May 31 '18 at 07:04

1 Answers1

4

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

  • 1
    Nope. Even the Parent property of `sender as Button` is null. – ravi kumar May 30 '18 at 10:07
  • I can see many people have problems with getting the right controls from the VisualTreeHelper. I cannot test your problem here but have you tried to use 'LogicalTreeHelper.GetParent()'? Also I found a resource which could give you more insights: http://shrinandvyas.blogspot.com/2013/01/wpf-visualtreehelper-find-parent-of.html – Niels de Schrijver May 30 '18 at 11:26