【问题标题】:Changing WPF Button Background Image更改 WPF 按钮背景图像
【发布时间】:2016-07-12 11:43:30
【问题描述】:

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

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;

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

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

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

有什么建议吗?

谢谢, 霍华德

【问题讨论】:

  • 您的 xaml 悬停模板样式很可能继承自非悬停模板,而从代码中分配它只会更改非悬停按钮样式。
  • 看看这个answer 看看它是否适合你。

标签: c# wpf image button wpf-controls


【解决方案1】:

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

清单 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;
}

希望这会有所帮助。

【讨论】:

  • 谢谢。我将在 C# 代码中多次更改按钮的图像。因此,我不想将图像文件的名称放入 XAML 文件中。你知道如何在 C# WPF 代码中做到这一点吗?
  • 请参考扩展答案。最好的问候,
  • 我发现一般来说,如果xaml包含一个源图像文件,那么程序C#代码不能改变图像。与您的示例相同。无论有没有在 xaml 代码中指定的图像文件,它都不能阻止问题。我读过它与Chrome有关。这个页面听起来好像有解决方案:code.msdn.microsoft.com/windowsdesktop/… 但是,我无法让它工作 - 示例代码的 app.xaml 文件有问题。复制作者的 xaml 文件不起作用。没有它,简单的 style.axml 文件将无法工作。
  • 这段代码 sn-p 已经过测试并且工作正常。如果它是一个图像控件,那么它将是一个不同的故事(re;stackoverflow.com/questions/36203201/…)。不知道“它与 Chrome 有关系”是什么意思(这是一个 WPF 应用程序)。只需遵循此解决方案,它应该可以工作。最好的问候,
  • 如果您单击 code.msdn.microsoft.com 链接,您将看到作者正在处理我遇到的问题。他的解决方案看起来像一千行代码。正如我所说,如果我将图像文件源放入 xaml 文件,那么我的 C# 代码不可能将图像更改为不同的图像。没有编译时间,也没有运行时间错误——它不会发生。但是,我感谢你试图提供帮助。我可能会让数百名用户忍受这个小问题——这太先进了,无法强制 WPF 不执行其默认行为。
【解决方案2】:

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

一点背景:这是我大约十年前用 Java/Netbeans 编写的一个应用程序,这些年来它变得相当大。由于 Java/Netbeans 不再支持桌面应用程序的软件开发,我将其移植到 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)

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

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

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

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多