when I run this code the Item-object in my CustomControl becomes a System.Windows.Data.Binding containing nothing but null values but the DataContext becomes an MyClass object (which Items is populated with)
<UserControl x:Name="thisControl">
<Grid x:Name="LayoutRoot">
<ItemsControl ItemsSource="{Binding ElementName=thisControl,Path=Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:CustomControl Item="{Binding}" DataContext="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
My CustomControl class
public partial class CustomControl : UserControl
{
public CustomControl()
{
InitializeComponent();
}
public object Item { get; set; }
}
is there something i don’t know about ItemsControl?
this is written in Silverlight 4.0
Thanks in advance!
There is no need for you to be attempting to assign the custom control DataContext. The ItemsControl will take care of that for you.
Also your
CustomControlcode needs to specify theItemproperty as aDependencyPropertyfor the binding to work. Binding doesn’t work on plain ordinary properties.Example:
(I assume that
RSListBoxStopItemis a typo and you meant to generalise toCustomControl)