在按钮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";
    }
}


如何根据点击更新标签和图像?

最佳答案

给您的LabelImage命名。像这样:

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

10-04 12:50
查看更多