【问题标题】:WPF: LinearGradientBrush with Alpha not works in a Popup control?WPF:带有 Alpha 的 LinearGradientBrush 在弹出控件中不起作用?
【发布时间】:2012-07-18 10:54:42
【问题描述】:

我想创建一个自定义 WPF 下拉菜单,因此我创建了一个包含 ToggleButton 和 Popup 控件的用户控件。当我单击按钮时会出现弹出窗口。 现在我想在弹出的菜单项上添加鼠标悬停效果:但是 LinearGradientBrush 不适用于 alpha 通道:

<UserControl.Resources>
    ...
    <Style x:Key="SubMenuItemStyle" TargetType="{x:Type MenuItem}">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="MenuItem">
                    <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">
                        <Rectangle x:Name="rectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                               Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"
                               Fill="{TemplateBinding Background}" />
                        <StackPanel>
                            <TextBlock x:Name="text" Text="{TemplateBinding Header}" Foreground="{TemplateBinding Foreground}" />
                        </StackPanel>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Stroke" TargetName="rectangle" Value="#30000000"/>
                            <Setter Property="Fill" TargetName="rectangle">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,0" StartPoint="1,1">
                                        <GradientStop Color="#10000000" Offset="0"/>
                                        <GradientStop Color="#10FFFFFF" Offset="1"/>
                                        <!-- Not works... -->
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" TargetName="text" Value="#FF9A9A9A"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<Grid>
    <Controls:ToggleImageButton Style="{DynamicResource ArrowMenuStyle}" x:Name="imageButton" Height="21" />
    <Popup x:Name="popup" PlacementTarget="{Binding ElementName=imageButton}" Placement="Bottom" StaysOpen="False" IsOpen="{Binding ElementName=imageButton, Path=IsChecked}">
        <ItemsControl ItemsSource="{Binding MenuCommands}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <MenuItem Header="{Binding Name}" Command="{Binding Command}" Background="{Binding ElementName=popupMenuControl, Path=MenuBackground}" Foreground="{Binding ElementName=popupMenuControl, Path=MenuForeground}"  Click="MenuItem_Click" Style="{StaticResource SubMenuItemStyle}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Popup>
</Grid>

当我将 GradientBrush 设置为此时,一切正常:

    <LinearGradientBrush EndPoint="0,0" StartPoint="1,1">
              <GradientStop Color="Green" Offset="0"/>
              <GradientStop Color="Yellow" Offset="1"/>
    </LinearGradientBrush>

但似乎 Alpha 通道破坏了整个东西,我看到的是一个黑色矩形。 ...有什么想法吗?

如果我将 MenuItems 放在 Menu 容器中,Menu 会覆盖我的样式...

提前致谢!

【问题讨论】:

  • 您是否将AllowsTransparency 设置为true
  • 如果我将此设置为 true,则整个悬停项将是透明的。
  • 不一定,会和你设置的一样透明
  • 谢谢!现在它工作正常。 (我怎样才能接受您的评论作为答案?)

标签: wpf menu popup lineargradientbrush


【解决方案1】:

您是否将AllowsTransparency 设置为true

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    相关资源
    最近更新 更多