0

What I want: I'm trying to create a ComboBox which has a 'Default' element seperated from the rest of the items.

What I have: So by following the countless threads about grouping the items and overwritting the ItemTemplate I came to this result:

http://imgur.com/a/ztVWM

.CS

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ObservableCollection<GroupItem>()
            {
                new GroupItem() { Name = "Default Item", GroupName = "Default"},
                new GroupItem() { Name = "Item 1", GroupName = "Item"},
                new GroupItem() { Name = "Item 2", GroupName = "Item"},
                new GroupItem() { Name = "Item 3", GroupName = "Item"}
            };
        }

        public class GroupItem
        {
            public string Name { get; set; }
            public string GroupName { get; set; }
        }
    }
}

XAML

<Window.Resources>
    <CollectionViewSource Source="{Binding}" x:Key="GroupedDataCollView">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="GroupName" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
    <DataTemplate x:Key="GroupHeaderTemplate">
        <TextBlock Text="{Binding GroupName}" Margin="10,0,0,0" Foreground="#989791" />
    </DataTemplate>
    <!--Here, we tell that the URL's description should be displayed as item text.-->
    <DataTemplate x:Key="NameTemplate">
        <TextBlock Text="{Binding Name}" Margin="10,0,0,0"/>
    </DataTemplate>

    <Style x:Key="GroupStyleContainerStyle" TargetType="{x:Type GroupItem}" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <StackPanel>
                        <Separator />
                        <ItemsPresenter />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Window.Resources>
<Window>
<!-- ... -->
    <ComboBox Grid.Row="1" Grid.Column="1" 
              ItemsSource="{Binding Source={StaticResource GroupedDataCollView}}"
              Width="200" Margin="10">
        <ComboBox.GroupStyle>
            <GroupStyle 
                ContainerStyle="{StaticResource GroupStyleContainerStyle}"
                HeaderTemplate="{StaticResource GroupHeaderTemplate}" />
        </ComboBox.GroupStyle>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
<!-- ... -->
</Window>

The Problem: Since we use Dictionaries for styling and templates which lie in another project, I can't use this 'solution' because this would require the style to know what kind of items he has to expect (Destroys the isolation of the custom styles and templates). Furthermore it's not really a group that I want to create rather a 'special' item, which is seperated by the others visually.

So I tried to create a custom UserControl to solve the problem so I could use it like this:

<ComboBox ItemsSource="{Binding Items}" DefaultItem="{Binding DefaultItem}" SelectedItem="{Binding SelectedItem}" />

In this CustomControl I would create the necessary grouping and handling hidden from the user. But how can I do this? And how can I overwrite the whole selection behaviour since I don't want to return a Key-Value-Pair (GroupItem) instead of the item the user expects.

So in short: How can I create a CustomControl which provides the necessary functionality shown at the start combined with the simplicity shown above?

Hans Vader
  • 183
  • 10
  • https://stackoverflow.com/questions/8521152/key-value-pair-combobox-in-wpf – JWP Jun 07 '17 at 13:12
  • 2
    Dod you look into the CompositeCollection class?: https://stackoverflow.com/questions/44284074/wpf-combobox-binding-from-2-source/ – mm8 Jun 07 '17 at 13:25
  • Didn't know about it yet, but I'll check it out and post my results. Thank you – Hans Vader Jun 08 '17 at 06:36
  • Worked perfectly. Can't add the result since it's embedded into the company code and you know.. copyright and stuff. If I find the time I'll create a new project to demonstrate it and post it here. – Hans Vader Jun 20 '17 at 12:27

0 Answers0