我有一个自定义控件,其中包含一个ComboBox,我在其中创建了多个数据列。 这很好用,但我一直无法弄清楚如何将标题行标题到下拉列表的顶部,并为每个列添加标题。此外,如果可能的话,它希望能够设置它的样式,以便行交替颜色。鉴于下面的XAML,我对如何做到这一点的任何想法?
<UserControl x:Class="ActualsAllocations.LegalEntityDropDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="28" d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<Style x:Key="MainComboStyle" TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
</Style.Triggers>
</Style >
</ResourceDictionary>
</UserControl.Resources>
<DockPanel>
<Label Name="lblTitle" Width="75" Content="Title" Margin="3,3"/>
<ComboBox Name="cmbMain" HorizontalAlignment="Stretch" Margin="3,3" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource MainComboStyle}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=VersionID}" Width="25" Margin="3,0"/>
<TextBlock Text="{Binding Path=VersionName}" Width="220" Margin="3,0"/>
<TextBlock Text="{Binding Path=EndDate, StringFormat={}{0:M/yyyy}}" Width="50" Margin="3,0" />
<TextBlock Text="{Binding Path=CreatedByUserName}" Width="100" Margin="3,0"/>
<TextBlock Text="{Binding Path=CreateDate}" Width="120" Margin="3,0"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ComboBox>
</DockPanel>
</UserControl>
答案 0 :(得分:1)
关于行的替代颜色我会尝试使用 ItemTemplateSelector ,您可以阅读here。
对于标题,我会尝试使用描述为here的 CompositeCollection 。
如果你选择 ItemTemplateSelector ,你不需要为标题设置不同的模板,因为它已经是ComboBoxItem,而模板将被忽略。
ItemTemplateSelector的代码:
public class CustomTemplateSelector : DataTemplateSelector
{
public DataTemplate EvenTemplate { get; set; }
public DataTemplate OddTemplate { get; set; }
public CollectionViewSource Collection { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var list = Collection.Source as IList;
if (list != null)
{
if (list.IndexOf(item) % 2 == 0)
{
return EvenTemplate;
}
else
{
return OddTemplate;
}
}
return EvenTemplate;
}
}
用于组合框的Xaml:
<DockPanel>
<DockPanel.Resources>
<Style x:Key="MainComboStyle" TargetType="{x:Type ComboBoxItem}">
<Style.Triggers></Style.Triggers>
</Style >
<CollectionViewSource x:Key="list" Source="{Binding}"/>
<DataTemplate x:Key="EventTemplate">
<StackPanel Orientation="Horizontal" Background="Red">
<TextBlock Text="{Binding Path=VersionID}" Width="25" Margin="3,0"/>
<TextBlock Text="{Binding Path=VersionName}" Width="220" Margin="3,0"/>
<TextBlock Text="{Binding Path=EndDate, StringFormat={}{0:M/yyyy}}" Width="50" Margin="3,0" />
<TextBlock Text="{Binding Path=CreatedByUserName}" Width="100" Margin="3,0"/>
<TextBlock Text="{Binding Path=CreateDate}" Width="120" Margin="3,0"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="OddTemplate">
<StackPanel Orientation="Horizontal" Background="Green">
<TextBlock Text="{Binding Path=VersionID}" Width="25" Margin="3,0"/>
<TextBlock Text="{Binding Path=VersionName}" Width="220" Margin="3,0"/>
<TextBlock Text="{Binding Path=EndDate, StringFormat={}{0:M/yyyy}}" Width="50" Margin="3,0" />
<TextBlock Text="{Binding Path=CreatedByUserName}" Width="100" Margin="3,0"/>
<TextBlock Text="{Binding Path=CreateDate}" Width="120" Margin="3,0"/>
</StackPanel>
</DataTemplate>
<local:CustomTemplateSelector OddTemplate="{StaticResource OddTemplate}"
EvenTemplate="{StaticResource EventTemplate}"
Collection="{StaticResource list}"
x:Key="customTemplateSelector" />
</DockPanel.Resources>
<Label Name="lblTitle" Width="75" Content="Title" Margin="3,3"/>
<ComboBox Name="cmbMain" HorizontalAlignment="Stretch" Margin="3,3"
ItemTemplateSelector="{StaticResource customTemplateSelector}" >
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem IsEnabled="False" Foreground="Black"> VersionID | VersionName | EndDate | CreatedByUser | CreateDate</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource list}}"/>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</DockPanel>
您可以考虑制作样式标题,以便它更好。此外,您可以将部分此类数据模板放在单独的控件中,以避免重复代码。