首页 > 解决方案 > WPF 在固定位置放置标签

问题描述

我有一个带有平移和可缩放图像的 WPF 窗口。现在我想要一个标签,它将在窗口中的固定位置(例如在中心)显示缩放百分比。即使我缩放或平移照片,也不应更改位置。

这是我的窗口的 XAML:

<Window x:Class="ImageViewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ImageViewer"
    Name="mainWindow"
    mc:Ignorable="d"
    Title="MainWindow" WindowStyle="None"
    AllowsTransparency="True"
    WindowStartupLocation="CenterScreen"
    Height="600"
    Width="900"
    WindowState="Maximized">
<Window.Background>
    <SolidColorBrush Opacity="0.5" Color="#FF3C3C6A"/>
</Window.Background>
<Grid>
    <local:ZoomBorder x:Name="border" ClipToBounds="True">
        <Image Name ="imageContainer"/>
    </local:ZoomBorder>
</Grid> </Window>

现在我想以一种永远不会改变其位置的方式放置以下标签。

<Label Name ="ZoomLabel" Width="150" Height="50"
              Content="100%" HorizontalContentAlignment="Center"
              VerticalContentAlignment="Center"
              Background="#FF383838" Foreground="#FFEAE4E4" 
              FontWeight="Bold" Opacity="0.75" FontSize="30"/>

标签: c#.netwpfvisual-studio

解决方案


您将希望将标签添加到与网格中的缩放边框相同的行、列中,以便它覆盖:

<Grid>
    <local:ZoomBorder x:Name="border" ClipToBounds="True">
        <Image Name ="imageContainer"/>
    </local:ZoomBorder>
    <Label Name ="ZoomLabel" Width="150" Height="50"
              Content="100%" HorizontalContentAlignment="Center"
              VerticalContentAlignment="Center"
              Background="#FF383838" Foreground="#FFEAE4E4" 
              FontWeight="Bold" Opacity="0.75" FontSize="30"
              Margin="10,10,0,0"
              />
</Grid> </Window>

推荐阅读