WPF-ItemTemplate不能按预期运行 [英] WPF - ItemTemplate not acting as expected

查看:122
本文介绍了WPF-ItemTemplate不能按预期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UserControl,用于显示UIElement的列表.该控件由单个ItemsControl组成,其中ItemPanelTemplate切换为水平StackPanel,其ItemsSource绑定到由UserControl暴露的DependencyProperty上,并且其ItemTemplateUserControl.Resources中设置.

I have a UserControl which I'm using to display a list of UIElements. The control consists of a single ItemsControl with it's ItemPanelTemplate switched for a horizontal StackPanel, its ItemsSource bound to a DependencyProperty exposed by the UserControl and its ItemTemplate set in the UserControl.Resources.

一切正常,除了ItemTemplate从未应用且我不明白为什么.完整的源代码在下面.

Everything works fine except the ItemTemplate never get's applied and I can't see why. The full source is below.

UserControl.xaml-

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-

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();
    }
}

}

我这样在我的主窗口中使用控件-

I consume the control in my main window like this -

    <slider:ContentSlider >
    <slider:ContentSlider.Pages>
        <Button>1</Button>
        <Button>2</Button>
        <Button>3</Button>
        <Button>4</Button>
    </slider:ContentSlider.Pages>
</slider:ContentSlider>

按钮看起来不错,但不在200像素的正方形边框之内.

The buttons appear fine but not inside the 200px square border.

任何帮助都会得到极大的帮助. 谢谢.

Any help would be greatlly appriciated. Thanks.

推荐答案

这是因为它是UIElement的列表,因此仅当无法直接显示项目时才应用项目模板.

It's because it's a list of UIElement, the item template is only applied if the items can't be displayed directly.

这篇关于WPF-ItemTemplate不能按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆