How to change the contents of a button when clicking on Xaml

I have button content with the name Hold the button Once When I pressed the button The content of the button should change to resume, again, if the resume button is pressed, then the hold should be visible, please help me ...

XAML Code:

<Button Style="{StaticResource CommonButtonStyle}" Template="{DynamicResource GlassButton}" ToolTip="F9" Click="Hold_Click" PreviewKeyDown="Hold_PreviewKeyDown" Name="OK" Margin="1,49,25,0" Grid.Column="2" Grid.Row="13" Grid.RowSpan="2"> <StackPanel Style="{StaticResource ButtonStackPanel}"> <Image Style="{StaticResource CancelImages}" /> <TextBlock Text="{Loc lblHold}" Style="{StaticResource ButtonTextBlock}" /> </StackPanel> </Button> 
+4
source share
3 answers

You can try something like this in the Click event:

 private void holdResumeButton_Click(object sender, RoutedEventArgs e) { if ((string)holdResumeButton.Content == "Hold") holdResumeButton.Content = "Resume"; else holdResumeButton.Content = "Hold"; } 

XAML:

 <Button x:Name="holdResumeButton" Content="Hold" Click="holdResumeButton_Click"/> 
+7
source

Use ToggleButton :

  <Style x:Key="HoldOrResumeButton" TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}"> <Setter Property="Content"> <Setter.Value> <TextBlock Text="Hold"/> </Setter.Value> </Setter> <Style.Triggers> <DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource Mode=Self}}" Value="True"> <Setter Property="Content"> <Setter.Value> <TextBlock Text="Resume"/> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> 
+4
source

Perhaps you can use ToggleButton instead of Button so that you can bind to the IsChecked property. http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked.aspx

For instance:

  <ToggleButton x:Name="TB"> <ToggleButton.Style> <Style> <Style.Triggers> <Trigger Property="ToggleButton.IsChecked" Value="True"> <Setter Property="ToggleButton.Content" Value="Resume"></Setter> </Trigger> </Style.Triggers> </Style> </ToggleButton.Style> </ToggleButton> 
+1
source

Source: https://habr.com/ru/post/1490421/


All Articles