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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With