WPF 网格:矩形作为透明叠加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15967424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:34:32  来源:igfitidea点击:

WPF Grid: Rectangle as transparent overlay

wpf

提问by Marco

I have a canvas in WPF 4.5 and want to overlay it with a UserControl, which consists mainly of

我在 WPF 4.5 中有一个画布,想用一个 UserControl 覆盖它,它主要由

a Grid with labels and a semi-transparent rectangle as background:

带有标签和半透明矩形作为背景的网格:

<UserControl x:Class="Cwss.Tactical.Navigation.ObjectInfoView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:u="clr-namespace:Cwss.Utils.Converter"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300">
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Tactical/Styles/CommonStyle.xaml"></ResourceDictionary>
        <ResourceDictionary>
          <Style x:Key="AttrName"
                 TargetType="{x:Type Label}">
            <Setter Property="Foreground"
                    Value="White" />
            <Setter Property="FontSize"
                    Value="14"></Setter>
          </Style>
          <Style x:Key="AttrValue"
                 TargetType="{x:Type Label}">
            <Setter Property="Foreground"
                    Value="Yellow" />
            <Setter Property="FontSize"
                    Value="14"></Setter>
          </Style>
        </ResourceDictionary>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </UserControl.Resources>


  <Grid Width="300"
        Height="200">

    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Rectangle Panel.ZIndex="-1"
               Opacity=".5"
               Width="300"
               Height="200"
               Fill="Blue"
               Stroke="Blue"
               StrokeThickness="2"
               RadiusX="8"
               RadiusY="8">
    </Rectangle>

    <Label Style="{StaticResource AttrName}"
           Grid.Column="0"
           Content="Class"></Label>
    <Label Style="{StaticResource AttrValue}"
           Grid.Column="1"
           Name="ObjectKnowledge_Clas"
           Content="Hi"></Label>
    <Label Style="{StaticResource AttrName}"
           Grid.Column="0"
           Grid.Row="1"
           Content="Range"></Label>
    <Label Style="{StaticResource AttrValue}"
           Grid.Column="1"
           Grid.Row="1"
           Content="{Binding ObjectKnowledge.Range, Converter={u:RangetoStringConverter}}"></Label>
  </Grid>
</UserControl>

The strange thing for me is that the first label gets rendered over the rectangle Grid with Rectangle,

对我来说奇怪的是第一个标签在矩形上呈现带矩形的网格

but all other labels not. Thanks for letting me know what I am doing wrong here!

但所有其他标签都不是。感谢您让我知道我在这里做错了什么!

回答by Viv

Well your Rectangleis rendered as the First element in the Grid with Row=0and Column=0(Assumed as Default by Grid)

那么你Rectangle被渲染为网格中的第一个元素Row=0Column=0(假设为网格默认)

Switch your Rectangle to something like:

将您的 Rectangle 切换为以下内容:

<Rectangle Grid.RowSpan="4"
           Grid.ColumnSpan="2"
           Width="300"
           Height="200"
           Panel.ZIndex="-1"
           Fill="Blue"
           Opacity=".5"
           RadiusX="8"
           RadiusY="8"
           Stroke="Blue"
           StrokeThickness="2" />

Now you see the other labels.

现在您可以看到其他标签。

You should use Snoopwhich could have highlighted the issue for you like so

您应该使用Snoop,它可以像这样为您突出问题

enter image description here

在此处输入图片说明