My Development KB

Odds and ends related to software development

Silverlight: System.ArgumentException with DataGridTemplateColumns and UserControls with x:Name

Posted by Ben G on November 4, 2009

I had a UserControl that specified an x:Name value like this:

<UserControl x:Class="CatManagement.UI.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" x:Name="Me">

I assigned an x:Name because I was wanting to bind certain UI elements to custom properties defined on my user control. However, this caused a problem when I used this user control in a DataGridTemplateColumn like this:

<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:AssignUserControl DataContext="{Binding}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>

The problem was that the following exception was thrown whenever a second DataGrid row was inserted:

 

System.ArgumentException was unhandled by user code
Message=”Value does not fall within the expected range.”
StackTrace:
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.DataGrid.InsertDisplayedElement(Int32 slot, UIElement element, Boolean wasNewlyAdded, Boolean updateSlotInformation) ….
[snip]
The problem was the x:Name specified on my usercontrol. Apparently, that resulted in multiple items in the same visual tree sharing the same x:Name value. Removing that attribute solved the problem.

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.