【发布时间】:2017-02-26 17:10:36
【问题描述】:
我需要一种效果,让我的元素不可见,但它们实际上会产生阴影。是否有可能做到这一点,如果可以,怎么做?
编辑:如果这是不可能的,我可以将我的元素(图像和标签)用作masks,因此只有阴影可见。我正在追求一些剪影效果。
另一个编辑:
这是按钮本身:
<Button x:Name="login_button" Grid.Row="1" Grid.Column="1" Template="{DynamicResource TileTemplate}"
Margin="5,10,5,10">
<Grid>
<Grid.Effect>
<DropShadowEffect BlurRadius="0"
Color="{Binding ElementName=login_button, Path=BorderBrush.Color,UpdateSourceTrigger=PropertyChanged}"
Opacity="{Binding ElementName=login_button, Path=BorderBrush.Opacity,UpdateSourceTrigger=PropertyChanged}"
Direction="0" ShadowDepth="0">
</DropShadowEffect>
</Grid.Effect>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Image Source="pack://application:,,,/textures/logos/lock.png"
VerticalAlignment="Stretch" HorizontalAlignment="Left" Grid.Row="0"
Margin="10,10,10,10" Visibility="Visible" OpacityMask="White">
</Image>
<Label Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Bottom"
HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom"
Content="LOG IN" FontSize="40" FontFamily="CenturyGothicRegual"
Margin="10,10,10,10">
<!--<Label.Foreground>
<SolidColorBrush Color="{Binding ElementName=login_button, Path=BorderBrush.Color,UpdateSourceTrigger=PropertyChanged}"
Opacity="{Binding ElementName=login_button, Path=BorderBrush.Opacity,UpdateSourceTrigger=PropertyChanged}"/>
</Label.Foreground>-->
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="#FFD1A139" />
</Setter.Value>
</Setter>
<Setter Property="Opacity" Value="0.8" />
</DataTrigger>
<DataTrigger Binding="{Binding IsPressed, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="#FFF0A300" />
</Setter.Value>
</Setter>
<Setter Property="Opacity" Value="0.8" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</Grid>
</Button>
所以阴影效果应用在元素本身的下方,计划是只有刺眼的阴影是可见的。我的目标是用它来躲避图像着色(和导入各种图像)。
【问题讨论】:
-
当然,等一下
-
效果是控件的一部分,因此控件的可见性/不透明度会影响效果。有很多方法可以做到这一点,但一种可能是在按钮后面添加一个空的无边框控件并对该按钮产生影响。将大小等绑定到按钮,使其与按钮的宽度/高度相同......你明白了。或者您甚至可以显示隐藏按钮的内容(本例中为网格)而不是按钮本身,并对按钮产生影响。
-
实际上,在本例中,您将模板应用于按钮。在模板中是您想要网格的位置,在按钮上是您想要效果的位置。然后只需显示/隐藏模板。
-
好主意,尽管我使用了 Blend 中的
paths和shapes。我必须将颜色更改模板应用于我所有按钮的网格元素,但我做了我想做的事。还是谢谢!
标签: c# wpf xaml shadow effects