I have a UserControl that I use to display a list of UIElement s. The control consists of one ItemsControl , in which the ItemPanelTemplate switches to the horizontal StackPanel , its ItemsSource bound to the DependencyProperty , which is displayed by the UserControl and its ItemTemplate set in UserControl.Resources .
Everything works fine, except that ItemTemplate never applied, and I don't understand why. Full source below.
UserControl.xaml -
<UserControl x:Name="UC" x:FieldModifier="private" x:Class="ContentSliderControl.ContentSlider" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <UserControl.Resources> <DataTemplate x:Key="pageTemplate"> <Border CornerRadius="10" Padding="5" Height="200" Width="200" Background="#333"> <ContentControl Content="{Binding}"/> </Border> </DataTemplate> <ItemsPanelTemplate x:Key="template"> <StackPanel IsItemsHost="True" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"/> </ItemsPanelTemplate> </UserControl.Resources> <ItemsControl ItemsPanel="{StaticResource template}" ItemTemplate="{StaticResource pageTemplate}" ItemsSource="{Binding ElementName=UC,Path=Pages}"/>
UserControl.xaml.cs -
[ContentProperty("Pages")] public partial class ContentSlider : UserControl { public List<UIElement> Pages { get { return (List<UIElement>)GetValue(PagesProperty); }
}
I use the control in my main window as follows:
<slider:ContentSlider > <slider:ContentSlider.Pages> <Button>1</Button> <Button>2</Button> <Button>3</Button> <Button>4</Button> </slider:ContentSlider.Pages> </slider:ContentSlider>
The buttons look great, but not inside the square border of 200 pixels.
Any help would be appreciated. Thanks.
source share