在按钮StackPanel
中设置buttonPlay
以使其内容丰富,如
<Button Height="37" HorizontalAlignment="Left" Margin="222,72,0,0" Name="buttonPlay" Click="buttonPlay_Click">
<StackPanel Orientation="Horizontal">
<Image Source="play.jpg" Height="20" Width="27" />
<Label Padding="4" Height="27" Width="55" FontWeight="Bold" FontFamily="Times New Roman"> Play</Label>
</StackPanel>
</Button>
我想在单击按钮时更改标签和图像中的文本,在buttonPlay_Click
事件中我这样做,如果有其他条件,但是我不知道如何更改标签也不知道如何更改标签它的形象。
private void buttonPlay_Click(object sender, RoutedEventArgs e)
{
if (buttonPlay.Content.ToString() == "Play")
{
//....some actions
buttonPlay.Content = "Stop";
}
else
{
//.....some other actions
buttonPlay.Content = "Play";
}
}
如何根据点击更新标签和图片?
答案 0 :(得分:3)
最简单的方法是为子控件设置名称,例如buttonPlayImage
和buttonPlayLabel
,然后您可以使用.
运算符访问其属性。
private void buttonPlay_Click(object sender, RoutedEventArgs e)
{
if (buttonPlayLabel.Content == "Play")
{
buttonPlayLabel.Content = "Stop";
buttonPlayImage.Source = new BitmapImage(new Uri("stop.png"));
}
else
{
//other actions
}
}
对于名字:
<Image Name="buttonPlayImage" ... />
<Label Name="buttonPlayLabel" ... >Play</Label>
答案 1 :(得分:2)
为您的Label
和Image
提供姓名。像这样:
<StackPanel Orientation="Horizontal" Name="hh">
<Image Height="20" Width="27" Name="img" Source="play.jpg" />
<Label Padding="4" Height="27" Width="55" Name="lbl" FontWeight="Bold" FontFamily="Times New Roman">Play</Label>
</StackPanel>
private void buttonPlay_Click(object sender, RoutedEventArgs e)
{
if (lbl.Content.ToString() == "Play")
{
//....some actions
lbl.Content = "Stop";
BitmapImage btm = new BitmapImage();
btm.BeginInit();
btm.UriSource = new Uri("pack://application:,,,/WpfApplication1;component/Resources/stop.png");
btm.EndInit();
img.Source = btm;
}
else
{
//.....some other actions
lbl.Content = "Play";
}
}
答案 2 :(得分:0)
您应该为图像和标签指定名称,然后更新这两个源和内容。您不应该更新按钮Content,因为它将替换所有StackPanel,结果Image和Label全部消失。