我试图获得文本块的绝对位置,但我只是获得了之前元素的相对位置。
这是我的 XAML 代码:
<Grid Style="{StaticResource LayoutRootStyle}" x:Name="grid_wrapper">
<Grid.Background>
<ImageBrush ImageSource="Assets/background_menu.png" />
</Grid.Background>
<Grid x:Name="grid_main">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="430" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" x:Name="Sidebar">
<Grid.Background>
<SolidColorBrush Color="#000000" Opacity="0.5" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="grid_mainCaption">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Style="{StaticResource BackButtonStyleMain}" TabIndex="2" Margin="40,50,0,0" VerticalAlignment="Center" x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}"/>
<TextBlock x:Uid="Statistics" Margin="120,0,0,0" Text="Statistics" VerticalAlignment="Bottom" FontSize="33" Foreground="Gold" FontFamily="Assets/sui generis rg.ttf#Sui Generis"/>
</Grid>
<Grid x:Name="grid_captions" Grid.Row="1" Grid.Column="0" Margin="120,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" /> <!-- Spielstand zurücksetzen -->
</Grid.RowDefinitions>
<TextBlock Text="Spielstatistik" Grid.Row="0" FontSize="22" Foreground="Gold" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,70,0,70"/>
<TextBlock Text="Insgesamte Spielzeit" Grid.Row="1" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis"/>
<TextBlock Text="Dauer des letzten Spiels" Grid.Row="2" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Dauer des längsten Spiels" Grid.Row="3" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Dauer des kürzesten Spiels" Grid.Row="4" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Anzahl der gewonnenen Spiele" Grid.Row="5" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Anzahl der gewonnenen Turniere" Grid.Row="6" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Anzahl der verlorenen Spiele" Grid.Row="7" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<!-- I want to have the Y-Position of this element -->
<TextBlock x:Name="Spielstatistik" Text="Anzahl der verlorenen Turniere" Grid.Row="8" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Würfelstatistik" Grid.Row="9" FontSize="22" Foreground="Gold" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,70,0,70"/>
<TextBlock Text="Anzahl der Würfe" Grid.Row="10" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Anzahl der Pip" Grid.Row="11" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Pip pro Wurf" Grid.Row="12" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Eröffnungen gewonnen" Grid.Row="13" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Anzahl Päsche" Grid.Row="14" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Zwei Päsche hintereinander" Grid.Row="15" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Drei Päsche hintereinander" Grid.Row="16" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Vier Päsche hintereinander" Grid.Row="17" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
<TextBlock Text="Fünf Päsche hintereinander" Grid.Row="18" FontSize="16" FontFamily="Assets/sui generis rg.ttf#Sui Generis" Margin="0,20,0,0"/>
</Grid>
</Grid>
</Grid>
我想拥有名为“Spielstatistik”的文本块的绝对 Y 位置,我已经尝试过如下操作:
float StatisticsNew::GetYPositionFromElement(UIElement^ obj)
{
auto transform = obj->TransformToVisual(nullptr);
auto point = transform->TransformPoint(Point(0,0));
return point.Y;
}
int YPosition = (int)GetYPositionFromElement(this->Spielstatistik);
YPosition 的值为 20。但 20 只是与之前元素的相对位置(因为 margin-top 设置为 20)。
如何获取元素的绝对位置?