meds
meds

Reputation: 22916

Setting visual elements of a silverlight control from codebehind?

I'm working on a button control in silverlight which can be in various states. Each state has its own look, different colours and even different images on the button.

I was wondering how I could modify the visual properties of the control from codebehind?

Here's the xaml:

<Style x:Key="MyButton" TargetType="Button">
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Height" Value="22"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Name="RootElement">
                            <vsm:VisualStateManager.VisualStateGroups>
                                <vsm:VisualStateGroup x:Name="CommonStates">
                                    <vsm:VisualState x:Name="Normal"/>
                                    <vsm:VisualState x:Name="MouseOver">
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:01.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform). (TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.94"/>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:01.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform). (TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.94"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Disabled">
                                        <Storyboard/>
                                    </vsm:VisualState>
                                </vsm:VisualStateGroup>
                                <vsm:VisualStateGroup x:Name="FocusStates">
                                    <vsm:VisualState x:Name="Focused">
                                        <Storyboard/>
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Unfocused"/>
                                </vsm:VisualStateGroup>
                            </vsm:VisualStateManager.VisualStateGroups>
                            <Border CornerRadius="1,1,1,1" Background="{StaticResource BlueVerticalGradientBrush}" BorderBrush="Red" BorderThickness="1,1,1,1" x:Name="MouseOver" RenderTransformOrigin="0.5,0.5">
                                <Border.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Border.RenderTransform>
                                <ContentPresenter x:Name="contentPresenter" Margin="10,0,10,0" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="MinWidth" Value="65"/>
        </Style>

Upvotes: 2

Views: 967

Answers (2)

Rahul Soni
Rahul Soni

Reputation: 4968

Have you tried giving the element and x:Name and accessing properties in code behind with the name?

Upvotes: 2

Mark Hall
Mark Hall

Reputation: 54522

You may want to look into using the VisualStateManager.GoToState or the VisualStateManager.GoToElementState Methods. I am not aware personally of a method to modify a style that is defined in the xaml.

Upvotes: 1

Related Questions