簡體   English   中英

更改WPF按鈕背景圖像

[英]Changing WPF Button Background Image

我從這里閱讀了有關如何更改按鈕的背景圖像文件的優秀建議:

如何在C#WPF代碼中更改\\設置按鈕背景圖像?

尤其是以下代碼:

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

除了告訴視覺工作室,它還是一個“內容”文件和“始終復制”。

似乎可行,但是只要我的光標懸停在圖像上方,圖像就會消失。

如果我在xaml中指定圖像文件,則不會發生此光標懸停問題。 但是,我想從C#代碼更改圖像文件。

有什么建議嗎?

謝謝,霍華德

為了更改WPF Button背景圖像,例如,在MouseOver事件上從Images/ContentImage.png更改為Images/ContentImage1.png ,您可以添加包含Image控件的ControlTemplate ,並使用如以下XAML片段中所示的Trigger

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

總共九個按鈕重復此操作。 不同類中的串行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)

用戶可以左鍵單擊數字以增加頻率值,而右鍵單擊數字以減小頻率值。 鼠標滾輪也可以這樣做。 但是,我沒有顯示它的代碼。

第一個答案解決了這個問題,但是當在一個公共方法中被另一個類調用時,它是行不通的。 但是,這些信息很有用,並改變了我的互聯網搜索解決方案。

我一直在努力尋找解決方案的代碼,是的,可以對其進行改進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM