更改 WPF 按钮背景图像

Changing WPF Button Background Image

我从这里阅读了关于如何更改按钮背景图像文件的优秀建议:

How to change\set button background image in C# WPF code?

特别是以下代码:

var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("Images/ContentImage.png",UriKind.Relative));
button1.Background = brush;

除了告诉视觉工作室这是一个 "content" 文件和 "Copy Always".

好像可以,但是每当我的光标悬停在图片上时,图片就会消失。

如果我在 xaml 中指定图像文件,则不会出现此光标悬停问题。但是,我想从 C# 代码更改图像文件。

有什么建议吗?

谢谢, 霍华德

为了更改 WPF 按钮背景图像,例如,在 MouseOver 事件中从 Images/ContentImage.png 更改为 Images/ContentImage1.png,您可以添加包含 Image 控件的 ControlTemplate 并使用Trigger 如以下 XAML 片段所示:

清单 1. 使用 XAML 触发器

在 MouseOver 上更改按钮图像
<Button Name="button1">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Image Name="img1" Source="Images/ContentImage.png" />
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="img1"  
                            Property="Source"  
                            Value="Images/ContentImage1.png" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

另一个解决方案将允许更改

清单 2. 单击时更改按钮图像 (XAML)

    <Button Name ="button1" Click="button1_Click">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Image x:Name="image1">
                    <Image.Style>
                        <Style TargetType="{x:Type Image}">
                            <Setter Property="Source"  Value="Images/ContentImage.png" />
                        </Style>
                    </Image.Style>
                </Image>
            </ControlTemplate>
        </Button.Template>
    </Button>

清单 3. 背后的 C# 代码(button1.click 事件处理程序)

private void button1_Click(object sender, RoutedEventArgs a)
{
    Image _img= button1.Template.FindName("image1", button1) as Image;
    Style _imgStyle = new Style { TargetType = typeof(Image) };
     _imgStyle.Setters.Add(new Setter(Image.SourceProperty, new BitmapImage(new Uri(@"pack://application:,,,/YourAssemblyName;component//Images/ContentImage1.png"))));
    _img.Style = _imgStyle;
}

希望这可能有所帮助。

好的,我找到了一个可行的解决方案 - 基于网页 http://www.codeproject.com/Questions/634111/How-to-remove-Glow-of-Button-on-Mouse-hover-in-WPF

一点背景知识:这是我大约十年前用 Java/Netbeans 编写的一个应用程序,多年来它变得相当大。因为Java/Netbeans不再支持s/w桌面应用程序的开发,我正在将其移植到Visual Studio WPF C#。

xaml:

<Button x:Name="buttonDigit1"  HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="30" Height="50"
                        Click="buttonDigit1_Click" MouseRightButtonDown="buttonDigit1_RightClick" MouseWheel="buttonDigit1_MouseWheelMoved">
                    <Image Width="30" Height="50"></Image>
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Grid Background="{TemplateBinding Background}">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="MouseOver"/>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                            </Grid>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

对总共九个按钮重复此操作。 另一个 class 中的串行 i/o 线程通过串行端口从无线电读取数据。如果收音机的频率发生变化,它会调用此 window class 中的方法来更改显示的频率。

window中的C#代码class:

// class global variables
    static ImageBrush[] digitBrush = new ImageBrush[10];
    static BitmapImage[] digitImage = new BitmapImage[10];
    static String radioDigitStyle = " ";

    public void displayFrequency(String frequency0)
    {
        int frequency_int = HowardUtils.frequency_int(frequency0);
        String frequency_str = HowardUtils.frequency_str(frequency_int);
        textBoxFreqVFOA.Dispatcher.Invoke(new Action(() => {
            textBoxFreqVFOA.Text = frequency_str;
        }));

        // Display the frequency in the digits display on RADIO Control Dialog
        String frequency = frequency0.Replace(".", "");  // Eliminate "."s
        frequency = HowardUtils.removeLeadingZeros(frequency);// Elmiinate leading 0s
        while (frequency.Length < 10) {
            // Add leading zeros for 10 digits
            frequency = "0" + frequency;
        }
        if (Global.DEBUG_RADIO) Console.WriteLine("displayFrequency frequency='" + frequency + "'");

        String[] digit_str = new String[9];
        digit_str[0] = frequency.Substring(1, 1);
        digit_str[1] = frequency.Substring(2, 1);
        digit_str[2] = frequency.Substring(3, 1);
        digit_str[3] = frequency.Substring(4, 1);
        digit_str[4] = frequency.Substring(5, 1);
        digit_str[5] = frequency.Substring(6, 1);
        digit_str[6] = frequency.Substring(7, 1);
        digit_str[7] = frequency.Substring(8, 1);
        digit_str[8] = frequency.Substring(9, 1);
        if (Global.DEBUG_RADIO) {
            Console.WriteLine("displayFrequency digit_str=" +
                digit_str[0] + digit_str[1] + digit_str[2] +
                digit_str[3] + digit_str[4] + digit_str[5] +
                digit_str[6] + digit_str[7] + digit_str[8]);
        }
        int[] digit = new int[9];
        int i;
        for (i = 0; i < 9; i++) {
            if (digit_str[i] == " ") { digit_str[i] = "0"; }    // Convert blank to zero.
            digit[i] = Convert.ToInt32(digit_str[i]);
        }
        if (Global.DEBUG_RADIO) Console.WriteLine("debug - digit_str[i]='" + 
            digit_str[0] + digit_str[1] + digit_str[2] + " " +
            digit_str[3] + digit_str[4] + digit_str[5] + " " +
            digit_str[6] + digit_str[7] + digit_str[8] + "'"); //xxxxx

        if (radioDigitStyle != Global.Settings.GetRadioDigits()) {
            // radio digits style has changed.
            //ImageBrush[] digitBrush = new ImageBrush[10];
            for (i = 0; i < 10; i++) {
                digitBrush[i] = new ImageBrush();
            }

            switch (Global.Settings.GetRadioDigits())
            {
                case "Blue Black Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0BCKBLUE.GIF", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1BCKBLUE.GIF", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2BCKBLUE.GIF", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3BCKBLUE.GIF", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4BCKBLUE.GIF", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5BCKBLUE.GIF", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6BCKBLUE.GIF", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7BCKBLUE.GIF", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8BCKBLUE.GIF", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9BCKBLUE.GIF", UriKind.Relative));
                    break;
                case "Computer Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0COMPUTE.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1COMPUTE.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2COMPUTE.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3COMPUTE.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4COMPUTE.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5COMPUTE.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6COMPUTE.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7COMPUTE.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8COMPUTE.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9COMPUTE.gif", UriKind.Relative));
                    break;
                case "Black Yellow Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0BCK-YLW.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1BCK-YLW.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2BCK-YLW.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3BCK-YLW.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4BCK-YLW.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5BCK-YLW.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6BCK-YLW.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7BCK-YLW.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8BCK-YLW.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9BCK-YLW.gif", UriKind.Relative));
                    break;
                case "Black Turq Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0BCKTURQ.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1BCKTURQ.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2BCKTURQ.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3BCKTURQ.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4BCKTURQ.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5BCKTURQ.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6BCKTURQ.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7BCKTURQ.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8BCKTURQ.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9BCKTURQ.gif", UriKind.Relative));
                    break;
                case "Black Red Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0BCK-RED.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1BCK-RED.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2BCK-RED.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3BCK-RED.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4BCK-RED.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5BCK-RED.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6BCK-RED.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7BCK-RED.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8BCK-RED.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9BCK-RED.gif", UriKind.Relative));
                    break;
                case "Black Pink Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0BCKPINK.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1BCKPINK.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2BCKPINK.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3BCKPINK.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4BCKPINK.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5BCKPINK.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6BCKPINK.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7BCKPINK.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8BCKPINK.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9BCKPINK.gif", UriKind.Relative));
                    break;
                case "Black Orange Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0BCKORNG.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1BCKORNG.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2BCKORNG.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3BCKORNG.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4BCKORNG.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5BCKORNG.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6BCKORNG.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7BCKORNG.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8BCKORNG.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9BCKORNG.gif", UriKind.Relative));
                    break;
                case "Black Old Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0BCK-OLD.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1BCK-OLD.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2BCK-OLD.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3BCK-OLD.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4BCK-OLD.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5BCK-OLD.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6BCK-OLD.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7BCK-OLD.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8BCK-OLD.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9BCK-OLD.gif", UriKind.Relative));
                    break;
                case "EVA01 Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0EVA00.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1EVA00.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2EVA00.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3EVA00.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4EVA00.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5EVA00.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6EVA00.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7EVA00.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8EVA00.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9EVA00.gif", UriKind.Relative));
                    break;
                case "DigitF Digits":
                    digitImage[0] = new BitmapImage(new Uri("digits/0DIGIF.gif", UriKind.Relative));
                    digitImage[1] = new BitmapImage(new Uri("digits/1DIGIF.gif", UriKind.Relative));
                    digitImage[2] = new BitmapImage(new Uri("digits/2DIGIF.gif", UriKind.Relative));
                    digitImage[3] = new BitmapImage(new Uri("digits/3DIGIF.gif", UriKind.Relative));
                    digitImage[4] = new BitmapImage(new Uri("digits/4DIGIF.gif", UriKind.Relative));
                    digitImage[5] = new BitmapImage(new Uri("digits/5DIGIF.gif", UriKind.Relative));
                    digitImage[6] = new BitmapImage(new Uri("digits/6DIGIF.gif", UriKind.Relative));
                    digitImage[7] = new BitmapImage(new Uri("digits/7DIGIF.gif", UriKind.Relative));
                    digitImage[8] = new BitmapImage(new Uri("digits/8DIGIF.gif", UriKind.Relative));
                    digitImage[9] = new BitmapImage(new Uri("digits/9DIGIF.gif", UriKind.Relative));
                    break;
            } //end switch 
            for (i=0; i<10; i++) {
                digitBrush[i] = new ImageBrush( digitImage[i] );
            }
            //
            radioDigitStyle = Global.Settings.GetRadioDigits();
        } // end of radio digits style has changed.

        System.Windows.Controls.Button[] radioDigitButtons = {
            buttonDigit1,
            buttonDigit2,
            buttonDigit3,
            buttonDigit4,
            buttonDigit5,
            buttonDigit6,
            buttonDigit7,
            buttonDigit8,
            buttonDigit9 };

        for (i = 0; i < 9; i++) {
            buttonDigit1.Dispatcher.Invoke(new Action(() =>
            {
                radioDigitButtons[i].Background = digitBrush[digit[i]];
            }));
        }
        RadioUtils.DisplayVFOFrequency();
    } // end displayFrequency(String frequency0)

用户可以左键点击数字增加频率值,右键点击数字减少频率值。鼠标滚轮也可以做同样的事情。但是,我没有显示它的代码。

第一个答案解决了问题,但是当在 public 方法中从不同的 class 调用时就不起作用了。但是,这些信息很有用,改变了我在互联网上搜索解决方案的方法。

几天来我一直在研究这段代码以找到解决方案,是的,它可以改进。