简体   繁体   English

UWP VisualTreeHelper.GetParent()返回null

[英]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.

相关问题 如何在VisualTreeHelper.GetParent()更改时将属性附加到控件或使用会更新的绑定? - How can I attach a property to a control or use a binding that updates whenever VisualTreeHelper.GetParent() changes? UWP VisualTreeHelper错误的参数参考 - UWP VisualTreeHelper Wrong Parameter Reference 为什么VisualTreeHelper.GetChildrenCount()为Popup返回0? - Why VisualTreeHelper.GetChildrenCount() returns 0 for Popup? WPF visualtreehelper.getchildrencount在datagrid上返回0 - WPF visualtreehelper.getchildrencount returns 0 on datagrid uwp combobox ContainerfromIndex(0) 总是返回 null - uwp combobox ContainerfromIndex(0) always returns null VisualTreeHelper.GetChildrenCount返回错误的计数Wp8 - VisualTreeHelper.GetChildrenCount returns wrong count Wp8 为什么VisualTreeHelper.GetChildrenCount返回0个顶级控件? - Why VisualTreeHelper.GetChildrenCount returns 0 top-level controls? C#UWP CryptographicEngine.Encrypt()返回null - C# UWP CryptographicEngine.Encrypt() returns null 在UWP中将JSON反序列化后,计算出的Sharepoint值返回Null - Calculated Sharepoint Value Returns Null after JSON Deserialize in UWP UWP App GpioController.GetDefault()始终返回null - UWP App GpioController.GetDefault() always returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM