Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf datagrid automatically expand first group

I have a datagrid with the itemsource bound to a ListCollectionView with one group. When i fill the collection, i want the first group autmatically viewed as expanded, how to code that in wpf (codebehind or mvvm)?

<DataGrid 
     ItemsSource="{Binding ResultColl}" 
     SelectedItem="{Binding Path=SelectedResultItem, Mode=TwoWay}"
     SelectionMode="Single" IsReadOnly="True" >
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <StackPanel>
                                                <TextBox Text="{Binding Items[0].ID}" />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>

    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Binding="{Binding Path=Typ}"/>
        <DataGridTextColumn Binding="{Binding Path=Info}"/>
        <DataGridTextColumn Binding="{Binding Path=orderDate, StringFormat={}{0:dd-MM-yyyy}}"/>
    </DataGrid.Columns>
</DataGrid>

In the mvvm controller:

ListCollectionView tmp = new ListCollectionView(myList);
tmp.GroupDescriptions.Add(new PropertyGroupDescription("ID"));
ResultColl = tmp;
...
ListCollectionView _resultColl;
public ListCollectionView ResultColl
{
    get { return _resultColl; }
    set { _resultColl = value;

        RaisePropertyChanged("ResultColl");
        if (value != null && _resultColl.Count > 0)
            SelectedResultItem = _resultColl.GetItemAt(0) as ItemResult;
    }
}

When executing the code, the datagrid is filled the 1st item is selected but group is collapsed.

like image 560
alexn234 Avatar asked Jan 14 '13 11:01

alexn234


2 Answers

Add IsExpanded property to your class and add binding to Expander:

<Expander IsExpanded="{Binding Items[0].IsExpanded}">

Set IsExpanded for first to true

like image 129
chameleon Avatar answered Nov 09 '22 20:11

chameleon


you can try add another bool property to your View Model defaulted to true but switching to false when first time used. And bind IsExpanded property of Expander to this with OneTime mode.

    public bool IsExpanded
    {
        get
        {
            if (_isExpanded)
            {
                _isExpanded = false;
                return true;
            }
            return false;
        }
    }

Xaml would be like that:

<Expander IsExpanded="{Binding DataContext.IsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Mode=OneTime}">
like image 21
Mr.Erlang Avatar answered Nov 09 '22 18:11

Mr.Erlang