Skip to main content

Microsoft Silverlight

Answered Question Using UserControl as ItemTemplate with ItemsControl cause AccessViolationExceptionRSS Feed

(0)

Guillaume R.
Guillaum...

Member

Member

34 points

14 Posts

Using UserControl as ItemTemplate with ItemsControl cause AccessViolationException

Hello,
Here's a simple scenario that fails in silverlight 2.0 beta 1 :

  • Create a UserControl, MyUC, with for exemple only a TextBlock with the Text "TPL" (inside the existing Grid).
  • In your main xaml (Page.xaml) add an ItemsControl and define the ItemTemplate as a DataTemplate containing only one instance of MyUC, you don't have to add binding.
  • In Page_Load, assign the ItemSource to "new string[] {"1", "2"};" for exemple.
  • Load the page and you got an AccessViolationException.

I've got a working solution showing the bug if needed.

Of course, if i copy/paste the content on MyUC to the ItemTemplate there is no problem. No problem also if i declare a DataTemplate as a resource and bind the ItemTemplate to it.

Guillaume

Guillaume

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Using UserControl as ItemTemplate with ItemsControl cause AccessViolationException

Hello, this is the desired behavior. It's due to everything inside your parent StackPanel are in the same name scope.

 

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

Guillaume R.
Guillaum...

Member

Member

34 points

14 Posts

Re: Re: Using UserControl as ItemTemplate with ItemsControl cause AccessViolationException

Hello,
If this is the desired behavior, could you give me a sample code showing how i can use a UserControl inside an ItemTemplate ?

Thanks,

Guillaume

Guillaume

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Re: Using UserControl as ItemTemplate with ItemsControl cause AccessViolationException

Oh, sorry, I thought I was replying to another thread...

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

Guillaume R.
Guillaum...

Member

Member

34 points

14 Posts

Answered Question

Re: Re: Using UserControl as ItemTemplate with ItemsControl cause AccessViolationException

Hello,
I've found a "workaround" (or the way it was designed to be) : I've created a Silverlight Class Library, added my UserControl in it then referenced this library and this UserControl in ItemTemplate.

I still find it strange that I should pass by a external library for this to work...

Guillaume

Guillaume

kornfish
kornfish

Member

Member

6 points

6 Posts

Re: Re: Using UserControl as ItemTemplate with ItemsControl cause AccessViolationException

I have been trying to figure out this same issue for a few days now. You would think using a UserControl within a DataTemplate would be a fairly common scenario. This must be a bug and hopefully will be fixed in Beta 2.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities