我有一个`UserControl`,用于显示一个`UIElement`列表。该控件包括一个单独的`ItemsControl`,其`ItemPanelTemplate`切换为水平的`StackPanel`,其`ItemsSource`绑定到`UserControl`公开的一个`DependencyProperty`,并在`UserControl.Resources`中设置了其`ItemTemplate`。除了`ItemTemplate`从未应用之外,一切都正常。我不知道原因。完整源代码如下。
按钮显示正常,但不在200px的正方形边框内。
非常感谢您的帮助。 谢谢。
<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); }
//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>
按钮显示正常,但不在200px的正方形边框内。
非常感谢您的帮助。 谢谢。