We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to modify Style from codebehind

zstapic
zstapic asked
on
Medium Priority
7,232 Views
Last Modified: 2013-11-12
Hi, I have Style i apply to my object by using this code:

this.Style = new MapZone.Resources()["MapZonePushpinStyle"] as Style;

and it works just fine. But I want to modify this style before applying it from C# code. I set comments in the code below by the properties i want to change. How I can change them?
<ResourceDictionary x:Class="MapZone.Resources"  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:MZ="clr-namespace:MapZone"
             mc:Ignorable="d">
    <Style x:Key="MapZonePushpinStyle" TargetType="MZ:Pushpin">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="MZ:Pushpin">
                    <Grid Height="15" Width="13" RenderTransformOrigin="0.479,0.456" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,0">
                        <Grid.RenderTransform>
                            <ScaleTransform x:Name="_ScaleTransform" ScaleX="1" ScaleY="1" />
                        </Grid.RenderTransform>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid x:Name="EllipseContainer" Margin="2.75,4.001,0.062,0.999">
                            <Ellipse x:Name="EffectEllipse2" Margin="-5.751,-5.062,-4.249,-4.938" Fill="#FFFFBB00" Stroke="{x:Null}" Opacity="0"  d:LayoutOverrides="Height"/>
                            <Ellipse x:Name="EffectEllipse1" Margin="-2.125,-1,-0.875,-2"  Width="12.812" Opacity="0" Fill="#FFFFBB00" Stroke="{x:Null}" d:LayoutOverrides="Height"/>
                            <Ellipse x:Name="OuterEllipse" Width="8.8" Stretch="Fill" StrokeMiterLimit="12" StrokeDashCap="Flat" Opacity="0.7" Margin="0.313,0.563,0,0.624" HorizontalAlignment="Left" d:LayoutOverrides="Height" RenderTransformOrigin="0.5,0.5">
                            	<Ellipse.Fill>
                            		<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <!-- I need to modify these two colors-->
                            			<GradientStop Color="White"/>
                            			<GradientStop Color="White" Offset="1"/>
                            		</LinearGradientBrush>
                            	</Ellipse.Fill>
                                <Ellipse.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Ellipse.RenderTransform>
                                <Ellipse.Stroke>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <!-- I need to modify these two colors-->
                                        <GradientStop Color="White"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </LinearGradientBrush>
                                </Ellipse.Stroke>
                            </Ellipse>
                            <Ellipse x:Name="InnerEllipse" Margin="2.062,3,2.938,2" Stroke="{x:Null}" Fill="#FFFFFFFF" Opacity="0.3" d:Width="100" d:LayoutOverrides="Height" RenderTransformOrigin="0.5,0.5">
                                <Ellipse.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Ellipse.RenderTransform>
                            </Ellipse>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Open in new window

Comment
Watch Question

Adding x:Name as an attribute of the GradientStops in the xaml will help you reference them from the code.
eg:

<GradientStop x:Name="gs1" Color="White"/>

Author

Commented:
Nope, i can't reference it. Maybe because it is a Style... How should I reference it from C# code?
CERTIFIED EXPERT

Commented:
modify the style as shown below and check..i think it will work..not sure...
<ResourceDictionary x:Class="MapZone.Resources"  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:MZ="clr-namespace:MapZone"
             mc:Ignorable="d">
<SolidColorBrush x:Key="color1" Color= "#B8D6EF" />
<SolidColorBrush x:Key="color2" Color= "#B8D6EF" />
    <Style x:Key="MapZonePushpinStyle" TargetType="MZ:Pushpin">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="MZ:Pushpin">
                    <Grid Height="15" Width="13" RenderTransformOrigin="0.479,0.456" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,0">
                        <Grid.RenderTransform>
                            <ScaleTransform x:Name="_ScaleTransform" ScaleX="1" ScaleY="1" />
                        </Grid.RenderTransform>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid x:Name="EllipseContainer" Margin="2.75,4.001,0.062,0.999">
                            <Ellipse x:Name="EffectEllipse2" Margin="-5.751,-5.062,-4.249,-4.938" Fill="#FFFFBB00" Stroke="{x:Null}" Opacity="0"  d:LayoutOverrides="Height"/>
                            <Ellipse x:Name="EffectEllipse1" Margin="-2.125,-1,-0.875,-2"  Width="12.812" Opacity="0" Fill="#FFFFBB00" Stroke="{x:Null}" d:LayoutOverrides="Height"/>
                            <Ellipse x:Name="OuterEllipse" Width="8.8" Stretch="Fill" StrokeMiterLimit="12" StrokeDashCap="Flat" Opacity="0.7" Margin="0.313,0.563,0,0.624" HorizontalAlignment="Left" d:LayoutOverrides="Height" RenderTransformOrigin="0.5,0.5">
                            	<Ellipse.Fill>
                            		<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <!-- I need to modify these two colors-->
                            			<GradientStop Color="White"/>
                            			<GradientStop Color="White" Offset="1"/>
                            		</LinearGradientBrush>
                            	</Ellipse.Fill>
                                <Ellipse.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Ellipse.RenderTransform>
                                <Ellipse.Stroke>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <!-- I need to modify these two colors-->
                                        <GradientStop Color="{StaticResource color1}"/>
                                        <GradientStop Color="{StaticResource color2}" Offset="1"/>
                                    </LinearGradientBrush>
                                </Ellipse.Stroke>
                            </Ellipse>
                            <Ellipse x:Name="InnerEllipse" Margin="2.062,3,2.938,2" Stroke="{x:Null}" Fill="#FFFFFFFF" Opacity="0.3" d:Width="100" d:LayoutOverrides="Height" RenderTransformOrigin="0.5,0.5">
                                <Ellipse.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Ellipse.RenderTransform>
                            </Ellipse>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
 
//c# code will be
 
Resource c = new MapZone.Resources();
c["color1"] = new SolidColorBrush(Colors.Bkue);
c["color2"] = new SolidColorBrush(Colors.Yello);
 
this.Style = c["MapZonePushpinStyle"] ;

Open in new window

Author

Commented:
Not working. Won't even assign color defined in "color1" SolidColorBrush, and getting error for this one:

c["color1"] = new SolidColorBrush(Colors.Bkue);
c["color2"] = new SolidColorBrush(Colors.Yello);

"Operation not implemented"

With or without C# code, not working
Did you correct the typos? (Blue, Yellow)

Author

Commented:
Of course :)
I stumbled across this in the Silverlight documentation:

"Styles are write-once in Silverlight. You can set a style to override a built-in default style, but attempting to set the same style again will result in an exception. However, you can change the values of individual control properties that have been set within a style. For example, you can set the Control..::.Template property at runtime even if this property has been set by a style."

So you can't modify the style during runtime (or if you can, you can't after they have been applied to an object).

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.