This is a migrated thread and some comments may be shown as answers.

Change background property of shape in code behind

1 Answer 121 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Veteran
Iron
Michael asked on 14 Feb 2013, 11:33 AM
Hy telerik team,

i want to change the background of a shape in code behind.

I have a class "ShapeBase". In ShapeBase i have a boolean property "IsMouseOver".
If IsMouseOver is true i want to change the background. 
If it is false the background should be set to my default background.

Is that possible?
Setting the "Background" property doesn't work.

cheers mike!

1 Answer, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 19 Feb 2013, 09:34 AM
Hi mike,

I am not sure that I understand your requirements. Can you please elaborate on your scenario? Why do you need to change the background of the shape in code behind? As far as I understand you need to implement a custom visual state on mouse over. Please note that such behavior can be easily achieved by editing the default style of the RadDiagramShape. In your case this might be a better approach. You can find more information about extracting templates in this article.

For your convenience I extracted and edited the default template of the RadDiagramShape. Also, I implemented an animation in the MouseOver visual state. Please note that the animation will be executed only during the MouseOver visual state. This means that once the mouse isn't over the shape, the background color of the control will reflect its normal VisualState settings - basically the default background value.

I changed the MouseOver state of a RadDiagramShape by adding the following code:

<VisualState x:Name="MouseOver">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Duration="0"
                                       Storyboard.TargetName="Geometry"
                                       Storyboard.TargetProperty="Fill">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MyBrush}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

Please take a look at the attached project and let me know if it works for you.

All the best,
Pavel R. Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Diagram
Asked by
Michael
Top achievements
Rank 1
Veteran
Iron
Answers by
Pavel R. Pavlov
Telerik team
Share this question
or