WPF XAML DataTemplate多个项目作为子项

时间:2014-09-25 12:44:40

标签: c# wpf xaml

我正在使用WPF和C#开发一个应用程序。

我有一个StackPanel来显示一些自定义控件。

当我将ArchiveDateResultItem添加到StackPanel时,它就像魅力一样,但我的问题是,ArchiveDateResultItem包含 ArchiveColorItem 的列表,我想多次将ArchiveColorItem添加到此StackPanel内部的WrapPanel(您可以在下面的XAML代码中看到 “内容将来到这里” 文本。

<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Top" x:Name="spp">
            <StackPanel.Resources>
                <Style TargetType="{x:Type loc:ArchiveDateResultItem}">
                    <Setter Property="Template"> 
                        <Setter.Value>
                            <ControlTemplate>
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,5,5">
                                    <StackPanel Orientation="Vertical">
                                        <Image Width="270" Height="130" VerticalAlignment="Top" HorizontalAlignment="Left" Source="silinecek/mrseb-windows-8-metro-start-screen_25.gif" Margin="0,0,2,0"/>
                                        <StackPanel Orientation="Horizontal">
                                            <Label Content="{Binding Path=DesignName, RelativeSource={RelativeSource Mode=TemplatedParent}}" Foreground="White" HorizontalAlignment="Left" Width="157"/>
                                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"  Width="112">
                                                <Label Content="{Binding Path=ChannelCount, RelativeSource={RelativeSource Mode=TemplatedParent}}" Foreground="White" Padding="0,5"/>
                                                <Label Content=" CH," Foreground="White" Padding="0,5"/>
                                                <Label Content="{Binding Path=VariantCount, RelativeSource={RelativeSource Mode=TemplatedParent}}" Foreground="White" Padding="0,5"/>
                                                <Label Content=" MH" Foreground="White" Padding="0,5"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </StackPanel>
                                    <ScrollViewer Tag="clrWrp" Width="174" Height="154" Template="{DynamicResource AppleStyleScrollBarStyle}"  HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Visible">
                                        <WrapPanel Orientation="Horizontal" Width="156" DataContext="{Binding Path=ResultColors}">
                                            <WrapPanel.Resources>
                                                <Style TargetType="{x:Type loc:ArchiveColorItem}">
                                                    <Setter Property="Template">
                                                        <Setter.Value>
                                                            <ControlTemplate>
                                                                <Border BorderThickness="1" BorderBrush="Black" Width="37" Height="37" Margin="2,0,0,2">
                                                                    <Border BorderThickness="1" BorderBrush="White">
                                                                        <Rectangle Fill="{Binding Path=ColorBrush}"></Rectangle>
                                                                    </Border>
                                                                </Border>
                                                            </ControlTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </Style>
                                            </WrapPanel.Resources>
                                           *****loc:ArchiveColorItem CONTENT SHOULD COME HERE*****
                                        </WrapPanel>
                                    </ScrollViewer>
                                </StackPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </StackPanel.Resources>

我的课程如下:

public class ArchiveDateResultItem : Control
{
    public String DesignName { get; set; }
    public String VariantCount { get; set; }
    public String ChannelCount { get; set; }

    private ArchiveColorItemCollection _resultColors = new ArchiveColorItemCollection();
    public ArchiveColorItemCollection ResultColors
    {
        get
        {
            return _resultColors;
        }
    }
}

public class ArchiveColorItemCollection : List<ArchiveColorItem>
{
}

public class ArchiveColorItem : Control
{
    public SolidColorBrush ColorBrush { get; set; }
}

这是我的XAML代码,用于将此控件添加到屏幕。

<loc:ArchiveDateResultItem DesignName="Try ME!" ChannelCount="20" VariantCount="20">
                    <loc:ArchiveDateResultItem.ResultColors>
                        <loc:ArchiveColorItem ColorBrush="Red"></loc:ArchiveColorItem>
                        <loc:ArchiveColorItem ColorBrush="Red"></loc:ArchiveColorItem>
                        <loc:ArchiveColorItem ColorBrush="Red"></loc:ArchiveColorItem>
                    </loc:ArchiveDateResultItem.ResultColors>
                </loc:ArchiveDateResultItem>

当我添加这些行时,ArchiveDateResultItems显示在屏幕上,但是,我看不到ArchiveColorItem。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

而不是使用ScrollViewer的{​​{1}}使用WrapPanel并自定义它。试试这样的事情

ItemsControl