[英]UWP VisualTreeHelper.GetParent() returns null
I have a ContentDialog
which has a ListView
. 我有一个
ContentDialog
,它有一个ListView
。 This ListView's
DataTemplate Contains a Grid
and this Grid
has a Button
. 这个
ListView's
DataTemplate包含一个Grid
,这个Grid
有一个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: 这是我的Button Click事件处理程序:
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. 问题是
VisualTreeHelper.GetParent(sender as Button) as Grid
的变量VisualTreeHelper.GetParent(sender as Button) as Grid
总是在我的PC上返回null
。 But this same code when I deploy on my mobile works perfectly fine (ie, variable grid
gets assigned the correct value). 但是,当我在移动设备上部署时,相同的代码完全正常(即,可变
grid
被分配了正确的值)。
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) App min版本:build 14393 App目标版本:Build 15063 PC windows版本:Build 17134(版本1803)
Note: I've tried changing the App target version to 1803 but the problem remains. 注意:我已尝试将App目标版本更改为1803,但问题仍然存在。
As I understand from a different question there are several ways to get the parent of the VisualTreeHelper. 正如我从另一个问题中理解的那样,有几种方法可以获得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.
可能是在你的手机或PC上,在后台加载不同的东西,以便你可以找到网格对象的位置发生变化。
You could check this answer as a reference of what I stated above: FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently 您可以将此答案作为我上面所述的参考来检查: FrameworkElement.Parent和VisualtreeHelper.GetParent的行为方式不同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.