Select to view content in your preferred language

finding elements within controltemplate

1338
6
02-11-2011 08:59 AM
YinShi
by
Regular Contributor
Hi, following the selections example in the help, I have the following xmal. How do I tab into the stoke property of the Ellipse control and the To property of ColorAnimation? I would like to change those properties in code behind. I've tried graphic.Symbol.ControlTemplate.SetValue (Ellipse.StrokeProperty, new SolidColorBrush(Colors.Red)); and graphic.Symbol.ControlTemplate.GetValue(Ellipse.StrokeProperty);. In either case, I got an exception.

Thanks,
yin

<Grid.Resources>
            <esriSymbols:MarkerSymbol x:Key="SelectMarkerSymbol" >
                <esriSymbols:MarkerSymbol.ControlTemplate>
                    <ControlTemplate>
                        <Ellipse x:Name="Element" Width="15" Height="15" StrokeThickness="10"
         Stroke="Green">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected" />
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="Element"
            Storyboard.TargetProperty="(Ellipse.Stroke).(SolidColorBrush.Color)"
            To="Cyan" Duration="00:00:0.25"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Ellipse>
                    </ControlTemplate>
                </esriSymbols:MarkerSymbol.ControlTemplate>
            </esriSymbols:MarkerSymbol>   
        </Grid.Resources
0 Kudos
6 Replies
JenniferNery
Esri Regular Contributor
I think you can do make the Storyboard a Resource and change ColorAnimation in code but I don't know if that is a good idea.

I have not tried this but I think syntax is correct.
<Storyboard x:Key="Animation">
<ColorAnimation Storyboard.TargetName="Element" Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)" 
To="Yellow" Duration="00:00:0.25"/>
</Storyboard>

<!--same code as before for SelectMarkerSymbol except for Selected state-->
<VisualState x:Name="Selected" Storyboard="{StaticResource Animation}">


In code-behind:
 Storyboard sb = this.LayoutRoot.Resources["Animation"] as Storyboard;
 ColorAnimation colorAnimation = sb.Children[0] as ColorAnimation;
 colorAnimation.To = Colors.Red;


Why don't you just create a different Symbol if Selected state requires a different ColorAnimation?
0 Kudos
YinShi
by
Regular Contributor
Hi Jennifer,

Maybe I didn't explain myself clearly. I am looking for ways to enumerate all the controls within a control template in code behind. For example:

    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <esriSymbols:TextSymbol x:Key="textSymbol" >
                <esriSymbols:TextSymbol.ControlTemplate>
                    <ControlTemplate>
                        <TextBox Text="{Binding Path=Text}" BorderThickness="0"/>
                    </ControlTemplate>
                </esriSymbols:TextSymbol.ControlTemplate>
            </esriSymbols:TextSymbol>   
        </Grid.Resources>

I would like to find the textbox in code behind. Is there any way of enumerating the content of the control template?
0 Kudos
JenniferNery
Esri Regular Contributor
You may be able to use VisualTreeHelper with your ControlTemplate as described in this silverlight forum: http://forums.silverlight.net/forums/p/115213/262980.aspx
0 Kudos
YinShi
by
Regular Contributor
I tried VisualTreeHelper but it does not take a resource as a parameter. VisualTreeHelper works if the controltemplate is applied to a control but not to a textsymbol. Textsymbol has a controltemplate property. Is there any way I can query again textsymbol.controltemplate to get the active textbox at the run time? I want to interrogate the active textbox at the run time in order to set the text property of textsymbol.

Thanks,
yin
0 Kudos
RyanWoodham
New Contributor
I'm also struggling with this.

For me my xaml code is:
 <Application.Resources>
  <ControlTemplate x:Key="MediaFrameTemplate">
   <Grid x:Name="MediaElementContainer">
    <MediaElement x:Name="AppMediaElement" Volume="1.0" Visibility="Collapsed" AutoPlay="True" Source="/02 Beautiful.wma"/>
    <Grid x:Name="ClientArea">
     <ContentPresenter />
    </Grid>
   </Grid>
  </ControlTemplate>
 </Application.Resources>


In the code behind, I'm attempting to find the "AppMediaElement".
ControlTemplate template = (ControlTemplate) Application.Current.Resources["MediaFrameTemplate"] ;


Did you ever solve your problem Yin Shi? Perhaps I can apply the solution to mine.

Best,
Ryan
0 Kudos
dotMorten_esri
Esri Notable Contributor
You cannot access or modify properties within a ControlTemplate.
You would have to create a new ControlTemplate instance if you want to change it  (use XamlReader.Load(xamlString) for this).
0 Kudos