c# - 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"/>
解决方案
您将希望将标签添加到与网格中的缩放边框相同的行、列中,以便它覆盖:
<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>
推荐阅读
- json - 为切片没有长度的结构切片赋值
- c++ - 有没有办法通过它们的位置编号来操作函数参数?
- c - 不同的进程可以在同一个目录下同时创建不同的文件吗?
- laravel - 在 Laravel 控制器中使用锚标记调用视图
- python - 为什么我不能从另一个对象更改实例变量
- flutter - Flutter:从具有正确重叠的矩阵变换中布局具有不同 z 坐标的多个子级
- pytorch - PyTorch Geometric 的手动小批量生成
- javascript - 如何在反应中创建动态css类
- javascript - Animate.css 动画 fadeInDown 在添加 @angular/material 后停止工作
- mysql - SQL:显示两个表的结果