lundi 29 juin 2015

ComboBox with null GroupDescription

I try to build an UI element witch is a ComboBox with group.

I'm almost done, here how I do it.

The data context implements this:

public interface ISelectionEditorViewModel : IEditorViewModel
{
    IEnumerable<ISelectionItemViewModel> AvailableItems { get; }
    ISelectionItemViewModel SelectedItem { get; set; }
}

Where ISelectionItemViewModel is:

public interface ISelectionItemViewModel
{
    string Group { get; }
    string Label { get; }
    ...
}

My xaml looks like this:

<UserControl.Resources>
    <CollectionViewSource x:Key="GroupedData" Source="{Binding Path=AvailableItems}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Group"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource GroupedData}}" SelectedValue="{Binding Path=SelectedItem}">
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="vm:ISelectionItemViewModel">
            <TextBlock Text="{Binding Path=Label}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

It's works like a charm.

Nice grouped items

But in some cases my items are not in any group. That it, for all item the group is null.

In this case, all items are still grouped. But the group label is empty.

Null group is nul

I try to get out of it by using a HeaderTemplateSelector in GroupStyle. The result is that if all items have null for group, the header will not be shown. I just have a list of items with no group. Ok nice.

No empty label but...

But I still have an issue, the items are not left aligned !! I would like to have this:

That it.

Aucun commentaire:

Enregistrer un commentaire