相关疑难解决方法(0)

WPF - ItemTemplate未按预期运行

我有一个UserControl我用来显示UIElements 的列表.控件包含一个单独的ItemsControl,它被ItemPanelTemplate切换为一个水平StackPanel,它被ItemsSource绑定到一个DependencyProperty暴露的UserControl和它的ItemTemplate设置UserControl.Resources.

一切都很好,除了ItemTemplate永远不会得到应用,我不明白为什么.完整的来源如下.

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}"/>
Run Code Online (Sandbox Code Playgroud)

UserControl.xaml.cs -

[ContentProperty("Pages")]
public partial class ContentSlider : UserControl
{


    public List<UIElement> Pages
    {
        get { return (List<UIElement>)GetValue(PagesProperty); …
Run Code Online (Sandbox Code Playgroud)

wpf user-controls datatemplate itemtemplate itemspanel

4
推荐指数
1
解决办法
2725
查看次数