2011-10-08 203 views
2

我有一個具有多個事件的按鈕。我想在單擊按鈕時更改它的文本。Silverlight更改按鈕文本

XAML:

<Button HorizontalAlignment="Left" Height="114" Margin="33,58,0,0" VerticalAlignment="Top" Width="224" Style="{StaticResource GlassButtonStyle}" FontSize="26.667" Click="Button_Click_1" Name="button1"> 
      <StackPanel Orientation="Horizontal"> 
       <Rectangle Height="69" Stroke="Black" Width="69"> 
        <Rectangle.Fill> 
         <LinearGradientBrush EndPoint="0.004,1.044" StartPoint="0.739,0.25"> 
          <GradientStop Color="Black" Offset="0"/> 
          <GradientStop Color="White" Offset="1"/> 
         </LinearGradientBrush> 
        </Rectangle.Fill> 
       </Rectangle> 

       <ContentPresenter Content="Clicke me" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </StackPanel> 
</Button> 

C#代碼:

button1.Content = "Hello world"; 

這使得矩形消失,但我只想要更改的文本。我怎樣才能做到這一點?

回答

1

您需要修改Button的Control Template以始終顯示您的矩形。

<Button HorizontalAlignment="Left" Height="114" Margin="33,58,0,0" VerticalAlignment="Top" Width="224" Style="{StaticResource GlassButtonStyle}" FontSize="26.667" Click="Button_Click_1" Name="button1" Content="Click Me"> 
<Button.Template> 
<ControlTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <Rectangle Height="69" Stroke="Black" Width="69"> 
        <Rectangle.Fill> 
         <LinearGradientBrush EndPoint="0.004,1.044" StartPoint="0.739,0.25"> 
          <GradientStop Color="Black" Offset="0"/> 
          <GradientStop Color="White" Offset="1"/> 
         </LinearGradientBrush> 
        </Rectangle.Fill> 
       </Rectangle> 

       <ContentPresenter Content="{TemplateBinding Content}" /> 
      </StackPanel> 

</ControlTemplate> 
</Button.Template> 
</Button> 

Using Control Templates to Customize a Control's Look and Feel