Search code examples
c#silverlightdependency-propertiesitemscontrol

ItemsControl inside my UserControl is not updating


I'm having trouble with my UserControl with following source code:

[ContentProperty("SetContent")]
public partial class HeaderContainer : UserControl
{
    // Header region

    public FrameworkElement SetContent
    {
        get { return (FrameworkElement)GetValue(SetContentProperty); }
        set { SetValue(SetContentProperty, value); }
    }

    public static readonly DependencyProperty SetContentProperty =
           DependencyProperty.Register("SetContent", typeof(FrameworkElement), 
                                       typeof(HeaderContainer), new PropertyMetadata(null));

    public HeaderContainer()
    {
        InitializeComponent();
        DataContext = this;
    }
}

This is my XAML

<StackPanel>
    <Border>
        <TextBlock Text="{Binding Header}" />
    </Border>
    <ContentPresenter Content="{Binding SetContent}"/>
</StackPanel>

And here is my Problem:

<c:HeaderContainer Header="List">
  <ItemsControl ItemsSource="{Binding ObjectList}" >
    <ItemsControl.ItemTemplate>
      <DataTemplate>
         <StackPanel>
           <TextBlock Text="{Binding DisplayName}"/>
           <TextBlock Text="{Binding SecondLine}" Foreground="Gray" />
         </StackPanel>
       </DataTemplate>
     </ItemsControl.ItemTemplate>
   </ItemsControl>
 </c:HeaderContainer>

The Itemscontrol itself is working, also the UserControl, when I add content in XAML. The problem is, when I add Items to ObjectList while it's in the HeaderContainer - nothing happens. Where am I thinking wrong?


Solution

  • It is hard to determine the exact issue here because your code is incomplete. You have a SetContent dependency property, however your example usage doe not use it. I think your approach should be to sub-class ContentControl, adding your Header property to this.

    Or ... just use the Silverlight Toolkit HeaderedContentControl which I think does exactly what you are trying to achieve!