Skip to main content
Home Forums Silverlight Programming Programming with .NET - General SL2: ItemsControl and nested FrameworkElements
1 replies. Latest Post by VFC on May 18, 2009.
(0)
VFC
Member
2 points
26 Posts
05-18-2009 11:22 AM |
I have extended the ItemsControl to have also an header, but when I add an FrameworkElement as child, which should displayed in the header, SL2 crash.
The header is rendered as a button, so the end design should be a FrameworkElement, for example a TextBlock, inside a Button. At the end of the Page I have added also a Button with inside a TextBox, which is rendered without problems. Why inside the ItemsControl it crashes?
Thanks in advance for any help.
Page xaml
<UserControl x:Class="SilverlightApplication18.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:SilverlightApplication18" Width="400" Height="300"> <StackPanel x:Name="LayoutRoot" Background="White"> <local:ItemsControlTest Header="Test"> <sys:String>Test1</sys:String> <local:ItemsControlTest Header="Test2"> <sys:String>Test3</sys:String> </local:ItemsControlTest> <!--<TextBox />--> <!-- This FrameworkElement crashes SL --> </local:ItemsControlTest> <Button HorizontalAlignment="Left" VerticalAlignment="Top"> <TextBox /> </Button> </StackPanel></UserControl>
ItemsControlTest.cs
namespace SilverlightApplication18{ public class ItemsControlTest : ItemsControl { public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(ItemsControlTest), null); public ItemsControlTest() { this.DefaultStyleKey = typeof(ItemsControlTest); } public object Header { get { return base.GetValue(ItemsControlTest.HeaderProperty); } set { base.SetValue(ItemsControlTest.HeaderProperty, value); } } protected override DependencyObject GetContainerForItemOverride() { return new ItemsControlTest(); } protected override bool IsItemItsOwnContainerOverride(object item) { return item is ItemsControlTest; } protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { ItemsControlTest test = element as ItemsControlTest; if (test != null && item != null && item != test) { test.Header = item; //test.Header = new TextBox(); //Adding a new FrameworkElement doesn't crash SL } //base.PrepareContainerForItemOverride(element, item); } }}
Themes/Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" xmlns:local="clr-namespace:SilverlightApplication18"> <Style TargetType="local:ItemsControlTest"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:ItemsControlTest"> <StackPanel> <Button Content="{TemplateBinding Header}" /> <ItemsPresenter Margin="10,0,0,0" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style></ResourceDictionary>
05-18-2009 12:08 PM |
I found the problem, at least it seems that the FrameworkElement.Parent is the origin of the crash, replacing it with the ItemControls will solve the problem:
protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { ItemsControlTest test = element as ItemsControlTest; if (test != null && item != null && item != test) { int num1 = this.Items.IndexOf(item); if (num1 != -1) this.Items[num1] = test; //Replacing the items with the ItemsControlTest test.Header = item; } //base.PrepareContainerForItemOverride(element, item); }