Kalyan
Kalyan

Reputation: 1393

ArgumentException: Value does not fall within the expected range

In my XAML page I have added some resource

<Page.Resources>
    <Button x:Key="btn" Content="Test Button"></Button>
    ...
</Page.Resources>

In my code file I am calling this resource but getting runtime exception. Cant understand what happens. Please advice

Button btn = this.Resources["btn"] as Button;
if (btn != null)
{
    MyPivotItem.Content = btn; // here I am getting the exception 
                               // "Value does not fall within the expected range"
}

Upvotes: 1

Views: 3089

Answers (1)

yasen
yasen

Reputation: 3580

You cannot have a FrameworkElement (a Control) in Resources. A control can only be a child of one other element, that is you cannot reuse it, just put it in one place and that's it. And if you put it as resource, it seems that it becomes child of the control in which you put it.

If you want to reuse some control, make a UserControl or Custom/Template Control and use it instead. Here's a comparison between the two types of controls: link

The links may not be the perfect tutorials, but I think they'll be a fine start.

Upvotes: 1

Related Questions