我有这个问题。
我有一个带4个按钮的网格(button1,button2,button3,button4)。
当我点击按钮时,我想从按钮显示图像,所以如果我点击Button2,我必须在Button2上显示图像。
那是我的代码
<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>
我们可以帮助我吗?
答案 0 :(得分:1)
您似乎正在使用WPF。您需要做的就是将图像设置为按钮内容。如果您需要Text + Image,则必须在内容中放置contianer并将标签和Image放入Container中。
将此代码放在ButtonClick事件中:
//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;
答案 1 :(得分:1)
根据“over”的含义,你可以这样做
<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>
并在你的button_click
中 private void button_click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (buttonPaura == button)
{
imagePaura.Visibility = System.Windows.Visibility.Visible;
}
}
你应该使用DataBinding而不是代码,但这只是一个建议