我有一个UserControl
我用它来显示列表UIElement
是。该控制由一个单一的ItemsControl
与它的ItemPanelTemplate
切换为水平StackPanel
, its ItemsSource
绑定到一个DependencyProperty
暴露于UserControl
和它的ItemTemplate
设置在UserControl.Resources
.
一切正常,除了ItemTemplate
从来没有得到应用,我不明白为什么。完整来源如下。
用户控件.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}"/>
用户控件.xaml.cs -
[ContentProperty("Pages")]
public partial class ContentSlider : UserControl
{
public List<UIElement> Pages
{
get { return (List<UIElement>)GetValue(PagesProperty); }
//set { SetValue(PagesProperty, value); }
}
// Using a DependencyProperty as the backing store for Pages. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PagesProperty =
DependencyProperty.Register("Pages", typeof(List<UIElement>), typeof(ContentSlider), new UIPropertyMetadata(null));
public ContentSlider()
{
InitializeComponent();
}
}
}
我像这样使用主窗口中的控件 -
<slider:ContentSlider >
<slider:ContentSlider.Pages>
<Button>1</Button>
<Button>2</Button>
<Button>3</Button>
<Button>4</Button>
</slider:ContentSlider.Pages>
</slider:ContentSlider>
按钮看起来不错,但不在 200 像素的方形边框内。
任何帮助将不胜感激。
谢谢。