2013-07-09 55 views
4
點擊了

我已經buttoncontent命名保留按鍵一旦當我點擊按鈕,按鈕的內容應更改爲恢復,再次按下如果恢復按鈕意味着保持應該是可見的,請幫我...如何更改按鈕的內容。當在XAML

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> 

回答

7

您可以嘗試在按鈕的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"/> 
+0

謝謝@ pcnthird –

+1

當XAML讓你這樣做時,使用代碼並不是一個好的解決方案。請參閱@Dennis解決方案。 – Askolein

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> 
4

使用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> 
相關問題