C# WPF XAML数据模板将多个项作为子项
我正在使用WPF和C开发一个应用程序 我有一个StackPanel来显示一些自定义控件 当我将ArchiveDateResultItem添加到StackPanel时,它的工作方式很有魅力,但我的问题是,ArchiveDateResultItem包含一个ArchiveColorItem列表,我想将该多次ArchiveColorItem添加到该StackPanel内的一个包装面板中(您可以看到“内容将在此处出现”下面XAML代码中的文本C# WPF XAML数据模板将多个项作为子项,c#,wpf,xaml,C#,Wpf,Xaml,我正在使用WPF和C开发一个应用程序 我有一个StackPanel来显示一些自定义控件 当我将ArchiveDateResultItem添加到StackPanel时,它的工作方式很有魅力,但我的问题是,ArchiveDateResultItem包含一个ArchiveColorItem列表,我想将该多次ArchiveColorItem添加到该StackPanel内的一个包装面板中(您可以看到“内容将在此处出现”下面XAML代码中的文本 <StackPanel Orientation="Ver
<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>
*****loc:ArchiveColorItem的内容应该在这里*****
我的课程是:
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; }
}
公共类ArchiveDatersUltItem:控件
{
公共字符串设计名称{get;set;}
公共字符串VariantCount{get;set;}
公共字符串ChannelCount{get;set;}
私有ArchiveColorItemCollection _resultColors=new ArchiveColorItemCollection();
公共ArchiveColorItemCollection结果颜色
{
得到
{
返回结果颜色;
}
}
}
公共类ArchiveColorItemCollection:列表
{
}
公共类ArchiveColorItem:控件
{
公共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>
当我添加这些行时,屏幕上会显示ArchiveDatereSultitem,但我看不到ArchiveColorItem
你能帮我吗?用
WrapPanel
代替ScrollViewer
使用ItemsControl
并对其进行自定义。尝试类似的方法
<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>
仍然不起作用。我已经删除了ScrollViewer和WrapPanel,并添加了您的代码,只做了一些修改。但结果是一样的:(顺便说一下,谢谢您的回答。)