Image Button in C# Code behind

二次信任 提交于 2019-12-11 10:07:13

问题


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

回答1:


Check the below links

http://blogs.msdn.com/b/knom/archive/2007/10/31/wpf-control-development-3-ways-to-build-an-imagebutton.aspx

http://www.hardcodet.net/2009/01/create-wpf-image-button-through-attached-properties

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/91df562f-414c-4326-ac65-42ef301b5f8f/




回答2:


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.



来源:https://stackoverflow.com/questions/5389769/image-button-in-c-sharp-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!