更改WPF按钮背景图像

时间:2016-03-24 18:42:18

标签: c# wpf image button wpf-controls

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

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

值得注意的是以下代码:

{% stylesheets filter='cssrewrite'
  'assets/css/style.css'
%}
  <link rel="stylesheet" type="text/css" href="{{ asset_url }}"/>
{% endstylesheets %}

除了告诉视觉工作室,它还是一个“内容”文件和“始终复制”。

它似乎有效,但每当我的光标悬停在图像上时,图像就会消失。

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

有任何建议吗?

谢谢, 霍华德

2 个答案:

答案 0 :(得分:0)

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

清单1.使用XAML触发器在MouseOver上更改按钮图像

Trigger

另一种解决方案是允许更改

清单2.点击(XAML)

更改按钮图像
<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>

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

    <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>

希望这可能会有所帮助。

答案 1 :(得分:0)

好的,我找到了一个可行的解决方案 - 基于网页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>

总共九个按钮重复此操作。 不同类中的串行I / O线程通过串行端口从无线电读取数据。如果收音机的频率发生变化,则会调用此窗口类中的方法来更改显示的频率。

窗口类中的C#代码:

// 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)

用户可以左键单击数字以增加频率值,然后右键单击数字以减小数值。鼠标滚轮也可以这样做。但是,我没有显示它的代码。

第一个答案解决了这个问题,但是当在一个公共方法内部从另一个类调用时,它就无法工作。但是,这些信息很有用,并改变了我的互联网搜索解决方案。

我几天来一直在努力寻找解决方案,是的,可以改进。