1

My current code is as follows:

           MyControl pvc = new MyControl ( );
           pvc.SetValue(FrameworkElement.NameProperty, "Control_Name");

           try
           {
              Grid.SetColumn( pvc, c );
              Grid.SetRow( pvc, r );
              layoutRoot.Children.Add( pvc );
           }
           catch( Exception excep )
           {
              System.Windows.MessageBox.Show( excep.ToString( ) );
           }

My issue is that I get an exception on the line I try to add the control to the layout (layoutRoot.Children.Add) and this exception only happens if I tried setting the nameproperty of the control, if I don't it works fine.

The exception is a 'System.ArgumentException: Value does not fal within expected range(...)

All i'm really trying to do is set the Name property of the control in codebehind instead of XAML (using x:Name="name"0

edit: The full exception is:

System.ArgumentException: Value does not fall within the expected range. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.Collection_Addvalue[T](PresentationFrameworkCollection'1Collection, CValue value) at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection'1 collection, DependencyObject value) at System.Windows.PresentationFrameworlCollection'1.AddDependencyObject(DependencyObject value) at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value) at System.Windows.PresentationFrameworkCollection'1.Add(T value) at Controls.MyControl.CreateLayout()

6
  • just added the entire error message... Commented Jun 30, 2011 at 3:19
  • Does it happen for all values or only specific ones? Commented Jun 30, 2011 at 6:27
  • @meds: Out of interest, why are you setting the names at all? They are normally only useful to Visual Studio (in generating the designer files). Commented Jun 30, 2011 at 7:56
  • @HiTech Magic I plan on retrieving them later and using their names seems like the best way to do it, that way I can give each a unique name.... Commented Jun 30, 2011 at 8:53
  • 1
    @meds: Better off adding your own attached property to hold "whatever you like". That's the sort of thing attached properties are designed for whereas the name property has an existing purpose. Commented Jul 1, 2011 at 10:57

1 Answer 1

0

Turns out the problem was that I was setting the same name multiple times, in hindsight that was a rather dumb thing to do on my part.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.