Select to view content in your preferred language

Define Graphics Selection Style at runtime?

809
3
01-24-2011 07:04 PM
JoeShmoe
Deactivated User
I'm new to Silverlight, but trying to figure out the cleanest way to define Graphics selection style at runtime.  The Selections example seems to indicate that this is done with control-templates.  It seems that defining control-templates at runtime requires XamlLoader - a rather inelegant solution.

I was hoping that someone could post an example to show me a cleaner way.

Thanks in advance!
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
In that sample, the symbols with control template are part of Resources. They define VisualStates for Selected and Unselected states. If the Graphic.Symbol uses a symbol such as the ones in that sample then at run-time, all that you have to do is update the Graphic.Selected property. I think this is the cleanest approach, you don't need XamlLoader.

You can also define a Renderer that use one of these symbols and apply that to your GraphicsLayer so you would not need to set each Graphic's Symbol as in that example.
<!-- place this under resources -->
<esri:SimpleRenderer x:Key="MySimpleRenderer" Symbols ="{StaticResource SelectMarkerSymbol}"/>
<!-- place this under Map.layers-->
<esri:GraphicsLayer ID="MyGraphicsLayer" Renderer="{StaticResource MySimpleRenderer}"/>


If you need to access these resources in code, you can do this
Symbol symbol = LayoutRoot.Resources["SelectMarkerSymbol"] as Symbol;
Renderer renderer = LayoutRoot.Resources["MySimpleRenderer"] as Renderer;
0 Kudos
JoeShmoe
Deactivated User
I thought about my ControlTemplate issue and realized I hadn't defined my problem fully. I didn't need custom properties.  I just didn't want my ControlTemplate XAML living stuck in some <resources> tag in some parent container.

Easy fix: I wrapped my custom ControlTemplate with a ResourceDictionary tag and put it into its own XAML file, similar to how styles are defined.  I load the ResourceDictionary via code, so users don't have to muck around with merging this dictionary in.

SimpleMarkerSymbol symbol = new SimpleMarkerSymbol {Color = brush, Size = 8};
MyCustomResourceDictionary dictionary = new MyCustomResourceDictionary ();
ControlTemplate controlTemplate = (ControlTemplate)dictionary["SelectableMarkerSymbolTemplate"];   symbol.ControlTemplate = controlTemplate;


I can see two other alternative solutions to my problem.  The most obvious is to simply create an alternative Symbol classes / XAML to use, which include any missing attributes.  Another option might be to nest a ContentControl/ContentPresenter inside the Symbol's ControlTemplate, and bind that to a custom visualization.
0 Kudos
dotMorten_esri
Esri Notable Contributor
XamlReader.Load is actually not that inelegant (apart from building the string of course). Creating a complex UI using XamlReader is faster than "building" it up using UI Elements one by one.

Anyway I'm glad you went the resourcedictionary route. Its the best way to do it. You might want to consider optimizing it a bit so you only load the resource dictionary the first time you need it, and reuse it after that.
0 Kudos