7

我有名为 Hold 按钮的按钮内容。当我单击按钮时,按钮内容应更改为 Resume,再次,如果按下恢复按钮意味着 Hold 应该可见。

XAML 代码:

 <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

3 回答 3

11

您可以在按钮的事件中尝试这样的Click事情:

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"/>
于 2013-07-09T05:20:40.480 回答
6

使用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>
于 2013-07-09T05:29:04.943 回答
1

也许你可以使用ToggleButton而不是Button这样你就可以绑定到IsChecked属性。 http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked.aspx

例如:

    <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>
于 2013-07-09T05:17:55.007 回答