[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to modify Style from codebehind

Posted on 2009-02-15
7
Medium Priority
?
7,179 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

0
Comment
Question by:zstapic
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:peterdungan
ID: 23654815
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"/>
0
 
LVL 4

Author Comment

by:zstapic
ID: 23657547
Nope, i can't reference it. Maybe because it is a Style... How should I reference it from C# code?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 23679976
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

0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 4

Author Comment

by:zstapic
ID: 23680896
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
0
 
LVL 9

Expert Comment

by:peterdungan
ID: 23682209
Did you correct the typos? (Blue, Yellow)
0
 
LVL 4

Author Comment

by:zstapic
ID: 23682433
Of course :)
0
 
LVL 9

Accepted Solution

by:
peterdungan earned 1500 total points
ID: 23686219
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).
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question