开发者

Unselectable Header with columns in WPF ComboBox

I have a custom control that contains a ComboBox in which I've created multiple data columns. This works great, but I have been unable to figure out how to head a header row to the top of the drop down with titles for each of the columns. Also, if possible it would love to be able to style it so that the rows alternate colors. Given the XAML below, any ideas of how I can do this?

<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>


About alternate colors for rows I would try using ItemTemplateSelector you can read about that here.

For header I would try using CompositeCollection described here.

If you go for this ItemTemplateSelector you don't need to different template for header as it will be already ComboBoxItem and Template will be ignored.

Code for 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 for combobox:

<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>

You can consider making styling header so it will be nicer. Also you can put part of this dataTemplates in separate control so you can avoid repeating code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜