【问题标题】:button Background transparent not set in WPF按钮背景透明未在 WPF 中设置
【发布时间】:2015-07-24 16:01:11
【问题描述】:

WPF:我有一个按钮,图像是在 c# 后面的代码中设置的

 btn.Content = new Image
                    {
                        Source = new BitmapImage(new Uri(MasterVariables.applicationPath + "Normal.png")),
                        Stretch = Stretch.Fill,
                    };

现在的问题是,当按钮进入按钮时,背景颜色设置为蓝色,但我在 mouse_enter 事件中更改了图像

 btn.BorderThickness = new Thickness(0);
            btn.Style = (Style)FindResource(ToolBar.ButtonStyleKey);
            btn.Background = Brushes.White; btn.BorderBrush = Brushes.Transparent;
           btn.Content = new Image
                    {
                        Source = new BitmapImage(new Uri(MasterVariables.applicationPath + "Hover.png")),
                        Stretch = Stretch.Fill,
                    };

图像发生了变化,但是当鼠标移到背景上时,背景会以蓝色突出显示

【问题讨论】:

  • 处理适当的事件然后更改属性,但最好的方法是从 xaml

标签: c# wpf


【解决方案1】:

更新您的样式以在 MouseOver 上触发背景:

<Style.Triggers>
    <Trigger Property='IsMouseOver' Value='True'>
        <Setter Property='Background' Value='White' />
    </Trigger>
</Style.Triggers>

【讨论】:

  • ismouseover 不起作用。还有其他想法吗?我需要在 c# 中更改而不是在 xmal 代码中
【解决方案2】:

试试这个

 <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" Value="Gray" TargetName="panel" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="LightGray" />
                            </Trigger>
                        </ControlTemplate.Triggers>

更多信息请参考本网站

How do you change Background for a Button MouseOver in WPF?

Transparent button background in WPF using style

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-05-19
  • 2014-06-20
  • 2019-03-18
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
相关资源
最近更新 更多