C# 中的图像按钮代码隐藏

发布于 2024-10-25 16:57:01 字数 2114 浏览 5 评论 0原文

我正在尝试在代码隐藏中设置图像背景。我尝试向按钮添加背景图像,但是当我将鼠标悬停在按钮上时,图像就会消失。为了解决这个问题,我必须编写函数来覆盖按钮行为,这在代码隐藏中要做的事情太多了。

然后我使用另一种方法,即将按钮和图像分别添加到网格单元格中。问题是——当我点击图像时,按钮不会触发。

如何使按钮具有悬停按下效果,即使鼠标悬停或按下按钮上的图像,而不是按钮的剩余区域按钮?

或者希望有人能给我建议更好的解决方案。下面是我的代码。

        InitializeComponent();
        Button playBtn = new Button();
        playBtn.Width = 60;
        playBtn.Height = 30;
        Image playIcon = new Image();
        playIcon.Source = new BitmapImage(new Uri(@"PATH"));
        playIcon.Stretch = Stretch.Uniform;
        playIcon.Height = 25;

        grid1.Children.Add(playBtn);
        grid1.Children.Add(playIcon);

        Grid.SetColumn(playBtn, 0);
        Grid.SetRow(playBtn, 0);
        Grid.SetColumn(playIcon, 0);
        Grid.SetColumn(playIcon, 0);

感谢大家的意见,在深入研究之后,它已经解决了。我所做的是将网格添加到Button.Content,然后将图像添加到网格。并使用OpacityIsEnable false状态添加灰色效果。下面我发布我的代码,希望有人觉得它有用或改进:

    Button playBtn = new Button();
    Image playIcon = new Image();

    public MainWindow()
    {
        InitializeComponent();

        Grid grid2 = new Grid();
        RowDefinition grid2_row1 = new RowDefinition();
        ColumnDefinition grid2_col1 = new ColumnDefinition();
        grid2.RowDefinitions.Add(grid2_row1);
        grid2.ColumnDefinitions.Add(grid2_col1);

        playBtn.Width = 60;
        playBtn.Height = 30;
        playBtn.Click += playBtn_Click;

        playIcon.Source = new BitmapImage(new Uri(@"pack://PATH..."));
        playIcon.Stretch = Stretch.Uniform;
        playIcon.Height = 25;

        playBtn.Content = grid2;
        grid2.Children.Add(playIcon);

        grid1.Children.Add(playBtn);

        Grid.SetRow(playIcon, 0);
        Grid.SetColumn(playIcon, 0);
    }

    public void playBtn_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Hello");
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        playBtn.IsEnabled = false;
        playIcon.Opacity = 0.3;
    }

I'm trying to set an image background in code-behind. I tried adding a background image to the button, but as soon as I hover over the button, the image disappears. To solve this, I have to write functions to override the button behavior, which is too much to do in code-behind.

I then use an alternative method, that is to add a button and an image separately to a grid cell. The issue is -- when I click on the image, the button won't trigger.

How do I make the button to have the hover and pressed effect, even when, the mouse is either hovering or presses the image on the button, but not the remaining area of the button?

Or hope someone can suggest me a better solution.Below is my code.

        InitializeComponent();
        Button playBtn = new Button();
        playBtn.Width = 60;
        playBtn.Height = 30;
        Image playIcon = new Image();
        playIcon.Source = new BitmapImage(new Uri(@"PATH"));
        playIcon.Stretch = Stretch.Uniform;
        playIcon.Height = 25;

        grid1.Children.Add(playBtn);
        grid1.Children.Add(playIcon);

        Grid.SetColumn(playBtn, 0);
        Grid.SetRow(playBtn, 0);
        Grid.SetColumn(playIcon, 0);
        Grid.SetColumn(playIcon, 0);

thanks for everyone input, after digging more into it, it sort of work out. What I did is add a Grid to Button.Content then add the image to the Grid. And using Opacity to add the grey out effect for IsEnable false state. Below I post my code, hope someone find it useful or improve on:

    Button playBtn = new Button();
    Image playIcon = new Image();

    public MainWindow()
    {
        InitializeComponent();

        Grid grid2 = new Grid();
        RowDefinition grid2_row1 = new RowDefinition();
        ColumnDefinition grid2_col1 = new ColumnDefinition();
        grid2.RowDefinitions.Add(grid2_row1);
        grid2.ColumnDefinitions.Add(grid2_col1);

        playBtn.Width = 60;
        playBtn.Height = 30;
        playBtn.Click += playBtn_Click;

        playIcon.Source = new BitmapImage(new Uri(@"pack://PATH..."));
        playIcon.Stretch = Stretch.Uniform;
        playIcon.Height = 25;

        playBtn.Content = grid2;
        grid2.Children.Add(playIcon);

        grid1.Children.Add(playBtn);

        Grid.SetRow(playIcon, 0);
        Grid.SetColumn(playIcon, 0);
    }

    public void playBtn_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Hello");
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        playBtn.IsEnabled = false;
        playIcon.Opacity = 0.3;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-11-01 16:57:01

WPF 中的按钮有不同的状态=“正常”、“鼠标悬停”和“按下”三种。

当您创建按钮时,您正在将其设置为“正常”状态。您还需要将“鼠标悬停”状态设置为也具有相同的图像。

Buttons in WPF have different states = "normal", "mouse over" and "pressed" are three.

When you create the button you are setting up it's "normal" state. You also need to set the "mouse over" state to have the same image as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文