Andrzej Gis
Andrzej Gis

Reputation: 14316

Silverlight change button text

I have a button with multiple contetnt. I want to change it's text when the button is clicked.

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# code:

button1.Content = "Hello world";

This makes the rectangle disappear, though I want only to change the text. How can I do this?

Upvotes: 2

Views: 2728

Answers (1)

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35156

You need to modify the Control Template of Button to show your rectangle all the time.

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

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

Upvotes: 1

Related Questions