WPF XAML DataTemplate 多个项目作为子项 [英] WPF XAML DataTemplate multiple items as children
问题描述
我正在使用 WPF 和 C# 开发应用程序.
I'm developing an app with using WPF and C#.
我有一个 StackPanel 来显示一些自定义控件.
I've a StackPanel to show some custom controls in.
当我将 ArchiveDateResultItem 添加到该 StackPanel 时,它的工作原理就像魅力一样,但是,我的问题是,ArchiveDateResultItem 包含一个 ArchiveColorItem 列表,我想多次将 ArchiveColorItem 添加到此 StackPanel 内的 WrapPanel(您可以在下面的 XAML 代码中看到 内容将出现在这里" 文本.
When I add ArchiveDateResultItem to that StackPanel it works like charm but, My problem is, ArchiveDateResultItem contains a list of ArchiveColorItem and I want to add that multiple times ArchiveColorItem to a WrapPanel inside of this StackPanel (You can see the "content will come here" text in the XAML code below.
<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 代码.
And this is my XAML code to add this controls to screen.
<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.
When I add these lines, ArchiveDateResultItems is shown on the screen but, I can't see ArchiveColorItem.
你能帮我解决这个问题吗?
Can you help me with that?
推荐答案
使用 ItemsControl
代替 ScrollViewer
和 WrapPanel
并对其进行自定义.尝试这样的事情
Instead of ScrollViewer
with WrapPanel
use an ItemsControl
and customize it. Try something like this
<ItemsControl ItemsSource="{Binding Path=ResultColors}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" Width="37" Height="37" Margin="2,0,0,2">
<Border BorderThickness="1" BorderBrush="White">
<Rectangle Fill="{Binding Path=ColorBrush}" />
</Border>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这篇关于WPF XAML DataTemplate 多个项目作为子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!