【问题标题】:UWP VisualTreeHelper.GetParent() returns nullUWP VisualTreeHelper.GetParent() 返回 null
【发布时间】:2018-11-03 13:07:35
【问题描述】:

我有一个ContentDialog,其中有一个ListView。这个ListView's DataTemplate 包含一个Grid,这个Grid 有一个Button。代码如下:

<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>

这是我的按钮单击事件处理程序:

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

问题是变量VisualTreeHelper.GetParent(sender as Button) as Grid 在我的电脑上总是返回null。但是,当我在移动设备上部署时,同样的代码工作得非常好(即变量grid 被分配了正确的值)。

更新: 这是我的 Live Visual Tree,它确认该按钮具有父网格。

应用最低版本:内部版本 14393 应用目标版本:Build 15063 PC windows版本:Build 17134(1803版)

注意:我已尝试将 App 目标版本更改为 1803,但问题仍然存在。

【问题讨论】:

  • 您想要解决方法还是想知道为什么它为空?变通办法是可行的,但答案更难
  • 一个正确的答案会很棒。但我还想补充一点,同样的代码过去可以正常工作。
  • 奇怪 - 我试过你的代码,似乎工作正常。你能检查var obj = VisualTreeHelper.GetParent(sender as Button); 是否返回任何东西吗?
  • 使用您可以使用的工具。转到 Debug/Windows/Live Visual Tree 并验证它。 Here's your list view containing 5 items, showing the first item with the button as a child of the grid, as expected.
  • @Romasz 返回 null。

标签: c# xaml uwp


【解决方案1】:

据我从另一个问题了解到,有几种方法可以获取 VisualTreeHelper 的父级。可能是在您的手机或 PC 上,在后台加载了不同的东西,以便您可以找到网格对象的位置发生变化。

你可以检查这个答案作为我上面所说的参考:FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently

【讨论】:

  • 不。甚至 sender as Button 的 Parent 属性也是空的。
  • 我可以看到很多人在从 VisualTreeHelper 获取正确的控件时遇到问题。我无法在这里测试您的问题,但您是否尝试过使用“LogicalTreeHelper.GetParent()”?我还发现了一个资源可以为您提供更多见解:shrinandvyas.blogspot.com/2013/01/…
猜你喜欢
  • 2020-09-28
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 2018-02-16
  • 2021-03-13
  • 2018-06-16
  • 1970-01-01
相关资源
最近更新 更多