bircastri
bircastri

Reputation: 153

How insert Image over a Button dynamically

I have this question.

I have a Grid with 4 Button (button1,button2,button3,button4).

When i click on a Button i would like to show an Image from a Button so if I click on Button2 i must display an Image over the Button2.

That s my code

<Grid x:Name="gridButton"
              Visibility="Visible">
            <Button Style="{DynamicResource GlassButton}" 
                Margin="50,0,0,306" 
                Name="buttonGioia" 
                Height="182" 
                HorizontalAlignment="Left"
                Width="414"
                Click="button_click">Gioia
            </Button>

            <Button Style="{DynamicResource GlassButton}" 
                Margin="0,0,50,306" 
                Name="buttonTristezza" 
                Height="182" 
                HorizontalAlignment="Right" 
                Width="414"
                Click="button_click">Tristezza
            </Button>

            <Button Style="{DynamicResource GlassButton}" 
                Margin="50,300,0,106" 
                Name="buttonPaura" 
                Height="182" 
                HorizontalAlignment="Left" 
                Width="414"
                Click="button_click">Paura
            </Button>

            <Button Style="{DynamicResource GlassButton}" 
                Margin="0,300,50,106" 
                Name="buttonRabbia" 
                Height="182"
                HorizontalAlignment="Right" 
                Width="414"
                Click="button_click">Rabbia
            </Button>
        </Grid>

Can we help me?

Upvotes: 1

Views: 781

Answers (2)

WiiMaxx
WiiMaxx

Reputation: 5420

Depending on what you mean by "over" you could do this

<Button Margin="50,300,0,106" 
        x:Name="buttonPaura" 
        Height="182" 
        HorizontalAlignment="Left" 
        Width="414"
        Click="button_click">
    <StackPanel>
        <Image x:Name="imagePaura" Source="your/image"  Width="414" Height="182"/>
        <Label>Content="Paura"</Label>
    </StackPanel>
</Button>

and in your button_click

    private void button_click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        if (buttonPaura == button)
        {
            imagePaura.Visibility = System.Windows.Visibility.Visible;
        }
    }

and You should use DataBinding instead of code behind but this is just an suggestion

Upvotes: 1

Christopher
Christopher

Reputation: 9804

You seem to be using WPF. There all you have to do is set a Image to be the buttons content. If you need Text+Image you instead have to put a contianer in the Content and put the label and Image into the Container.

Put this code in a ButtonClick event:

//Get the button instance that raised this event
Button Source = (Button)sender;

//Craete the Image isntance and set the image to be dispalyed
Image ButtonPicture = new Image();
ButtonPicture.BaseUri = "MyButtonPicture.jpg"

//Assign it to the buttons content
Source.Content = ButtonPicture;

Upvotes: 1

Related Questions