How to change button content when clicked in Xaml?

前端 未结 3 523
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 05:49

I have buttoncontent named Hold button. Once when I clicked button the button content should change to Resume, again if pressed resume button means Hold should be visible.

相关标签:
3条回答
  • 2021-01-18 06:02

    You can try something like this in the button's 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"/>
    
    0 讨论(0)
  • 2021-01-18 06:07

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

    For example:

        <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>
    
    0 讨论(0)
  • 2021-01-18 06:18

    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>
    
    0 讨论(0)
提交回复
热议问题