Welcome to MSDN Blogs Sign in | Join | Help

Emmanuel Mesas's Weblog

Live News - Windows Live, Xbox Live - what else?
Silverlight 2 Beta2 throws “Value does not fall within the expected range” when Transitioning States

Recently, as seen in my previous blog, I have been using intensively VisualStates and the VisualStateManager combined with new “Templating” contract introduced with TemplatePart and TemplateVisualState attributes.

Usually, the exception mentioned in my title occurs when you have multiple Xaml elements with the same x:Name. It is easy to find out in a UserControl on the Design Surface Visual Studio or Blend but more complex to detect when you are using Templates and store your stuff in the generic.xaml and App.xaml.

The problem I ran into was simple but difficult to troubleshoot. In fact, I was having a FormBase control (all in .cs code – no UI defined) and two derived controls (SignInForm and SignUpForm) which where using Parts from the base class (FormBase). I was using FormBase Parts (some ContentControls in my Template) to host UI that will participate to VisualStates – so no way that I could change the x:Name of those Parts when defining the Style.Template in my generic.xaml file otherwise they wouldn’t be found by the GetTemplateChild method. So, you end-up in the generic.xaml like this.

image

and had the same with a SignInForm.  With the yellow marker, I have highlighted what should not change because used by the GetTemplateChild method but surrounded controls like the one circled in red should have a different name for each template.

It might not be the perfect explanation for this behavior but this worked for me. When creating my Templates now, I make sure that all elements surrounding my TemplateParts have unique x:Name across all Styles declaration.

Posted: Friday, June 27, 2008 12:34 PM by emesas
Filed under: ,
Anonymous comments are disabled
Page view tracker