WPF DropShadow 消失

发布于 2024-08-22 12:06:49 字数 1521 浏览 6 评论 0原文

Wpf 阴影消失。 以下是如何重现。

在 xaml pad 中键入以下内容。

<Page.Resources>
    <DropShadowEffect x:Key="shadow"
        Opacity="1"
        ShadowDepth="1"
        Color="Blue"
        BlurRadius="30"/>
</Page.Resources>
<Grid>
    <Button  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border x:Name="Bd"
                                    BorderBrush="Black" BorderThickness="1"
                                    Background="Yellow"
                                    CornerRadius="8"
                                    Effect="{StaticResource shadow}">
                                <TextBlock Text="Hello out there"  HorizontalAlignment="Center" VerticalAlignment="Center"  />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
    </Button>
</Grid>

您应该看到一些带有边框的文本,并且边框周围有阴影。

现在将 Margin="0,0,0,0" 更改为 Margin="0,300,0,0",并调整 xaml pad 窗口的大小,以便您可以看到边界。在我的机器上,阴影现在消失了。

还有其他人看到这个吗?请帮忙。

Wpf dropshadow disappears.
Here is how to reproduce.

Type the following in xaml pad.

<Page.Resources>
    <DropShadowEffect x:Key="shadow"
        Opacity="1"
        ShadowDepth="1"
        Color="Blue"
        BlurRadius="30"/>
</Page.Resources>
<Grid>
    <Button  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border x:Name="Bd"
                                    BorderBrush="Black" BorderThickness="1"
                                    Background="Yellow"
                                    CornerRadius="8"
                                    Effect="{StaticResource shadow}">
                                <TextBlock Text="Hello out there"  HorizontalAlignment="Center" VerticalAlignment="Center"  />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
    </Button>
</Grid>

You should see some text with a border abound it, and a drop shadow around the border.

Now change the Margin="0,0,0,0" to Margin="0,300,0,0", and size your xaml pad window so you can see the border. On my machine, the drop shadow is now gone.

Anyone else see this? Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-08-29 12:06:49

我希望我能为您提供一个很好的解释,但是我在您的 XAML 中遇到了一些奇怪的事情,我想我可以为您提供解决方案。

  1. 如果您使用网格,您很可能想要布置特定数量的行和列。您应该指定这些。但是,这不会影响您的问题。
  2. 同样,您应该为元素指定行和列,因为无论如何您最终都需要将此信息放入 XAML 中。从 IMO 开始是一个好习惯。
  3. 我无法解释的问题是 Horizo​​ntalAlignment 和 VerticalAlignment 的组合。当您将按钮放入网格中时,我希望按钮占据整个空间,但事实并非如此。据我所知,实现这项工作的唯一方法是指定高度宽度。如果你这样做,效果就会起作用。我发现原始 XML 中的阈值是 Y 总边距为 239。例如,如果您使用 0,239,0,0,则会失败。如果你使用 0,139,0,100,它也会失败,因为总和是 239。奇怪的东西。

这是我的有效 XAML:

<Page.Resources>
    <DropShadowEffect x:Key="shadow"
        Opacity="1"
        ShadowDepth="2"
        Color="Blue"
        BlurRadius="30"/>
</Page.Resources>
<Grid Width="Auto" Height="Auto">
  <Grid.RowDefinitions>
    <RowDefinition></RowDefinition>
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition></ColumnDefinition>
  </Grid.ColumnDefinitions>
    <Button Width="90" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,300,0,0" Grid.Row="0" Grid.Column="0">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border x:Name="Bd"
                                    BorderBrush="Black" BorderThickness="1"
                                    Background="Yellow"
                                    CornerRadius="8"
                                    Effect="{StaticResource shadow}">
                                <TextBlock Text="Hello out there"  HorizontalAlignment="Center" VerticalAlignment="Center"  />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
    </Button>

编辑 OP 不想指定按钮的大小,因为按钮的内容可以动态更改。事实证明,如果将 MinHeight 设置为 18 之类的值(我认为这对于大多数内容来说是合理的),阴影效果将再次起作用。

<Border x:Name="Bd" BorderBrush="Black" BorderThickness="1" Background="Yellow" CornerRadius="8" Effect="{StaticResource shadow}" MinHeight="18">
  <StackPanel Orientation="Vertical">
    <TextBlock>hi</TextBlock>
    <TextBlock>there</TextBlock>
  </StackPanel>
</Border>

I wish I had a good explanation for you, but there were some weird things in your XAML that I played with and I think I have a solution for you.

  1. If you use a Grid, most likely you want to lay out a specific number of rows and columns. You should specify those. This doesn't affect your problem, however.
  2. Likewise, you should specify the Row and Column for your element because you'll eventually need to put this information in your XAML anyway. It's a good habit to start with IMO.
  3. The problem that I can't explain is with the combination of HorizontalAlignment and VerticalAlignment. When you put the Button in the Grid, I would expect the Button to take up the entire space, but it doesn't. The only way you can make this work as far as I could figure out was to specify Height and Width. If you do this, the Effect will work. I found that the threshold in your original XML was a total Y margin of 239. For example, if you used 0,239,0,0, it would fail. If you used 0,139,0,100, it would also fail because the sum is 239. Weird stuff.

Here's my XAML that works:

<Page.Resources>
    <DropShadowEffect x:Key="shadow"
        Opacity="1"
        ShadowDepth="2"
        Color="Blue"
        BlurRadius="30"/>
</Page.Resources>
<Grid Width="Auto" Height="Auto">
  <Grid.RowDefinitions>
    <RowDefinition></RowDefinition>
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition></ColumnDefinition>
  </Grid.ColumnDefinitions>
    <Button Width="90" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,300,0,0" Grid.Row="0" Grid.Column="0">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border x:Name="Bd"
                                    BorderBrush="Black" BorderThickness="1"
                                    Background="Yellow"
                                    CornerRadius="8"
                                    Effect="{StaticResource shadow}">
                                <TextBlock Text="Hello out there"  HorizontalAlignment="Center" VerticalAlignment="Center"  />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
    </Button>

EDIT The OP does not want to specify a size for the Button because the Content of the Button can change dynamically. It turns out that if you set the MinHeight to something like 18 (I think this is reasonable for most content), the dropshadow effect will work again.

<Border x:Name="Bd" BorderBrush="Black" BorderThickness="1" Background="Yellow" CornerRadius="8" Effect="{StaticResource shadow}" MinHeight="18">
  <StackPanel Orientation="Vertical">
    <TextBlock>hi</TextBlock>
    <TextBlock>there</TextBlock>
  </StackPanel>
</Border>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文