views:

14

answers:

0

I Want My ComboBox to Behave like The standard ListBox When my data is grouped ListCollectionView and an Expander.

I've Noticed that when I use the ListBox I can Move between the item groups and open\close the expandrs, but when I try to switch the ListBox to ComboBox, I get 'trapped' in the group i'm in - While using only the keyboard.

Xaml :

<Grid>

    <ComboBox  Name="Combo" HorizontalContentAlignment="center" HorizontalAlignment="Left" Width="223" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                    <TextBlock Margin="4" Text="{Binding Path=City}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>

        <ComboBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template" >
                                <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="true" BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock Text="{Binding Path=Name}"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter/>
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ComboBox.GroupStyle>
    </ComboBox>
</Grid>

C# :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<StateInfo> stateList = new List<StateInfo>();

        stateList.Add(new StateInfo("Maryland", "Baltimore"));
        stateList.Add(new StateInfo("Maryland", "Frederick"));
        stateList.Add(new StateInfo("Maryland", "Germantown"));
        stateList.Add(new StateInfo("Taxes", "Houston"));
        stateList.Add(new StateInfo("Taxes", "Auston"));
        stateList.Add(new StateInfo("Taxes", "Dallas"));
        stateList.Add(new StateInfo("Taxes", "San Antonio"));
        stateList.Add(new StateInfo("California", "Los Angeles"));
        stateList.Add(new StateInfo("California", "Sacramento"));

        Combo.ItemsSource = stateList;

        CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(Combo.ItemsSource);

        PropertyGroupDescription group = new PropertyGroupDescription("State");

        view.GroupDescriptions.Add(group);
    }

    public class StateInfo
    {
        public StateInfo(String state, String city)
        {
            State = state;
            City = city;
        }

        public String State

        { get; set; }

        public String City

        { get; set; }
    }
}

thanks, yonatan