2013-01-14 15 views
5

Mam datagrid ze źródłem przedmiotów związanym z ListCollectionView z jedną grupą. Po wypełnieniu kolekcji chcę, aby pierwsza grupa była automatycznie wyświetlana jako rozwinięta, jak ją kodować w kodzie wpf (codebehind lub mvvm)?WPF datagrid automatycznie rozwija pierwszą grupę

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

W regulatorze MVVM:

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

Podczas wykonywania kodu DataGrid jest wypełnione 1st element jest wybrany, ale grupa jest zwinięty.

+0

To jest zbyt ogólnikowe, co próbowałeś do tej pory? – akjoshi

+0

Próbowałem ustawić właściwość SelectedItem po ustawieniu widoku kolekcjonowania – alexn234

Odpowiedz

11

Dodaj właściwość IsExpanded do swojej klasy i dodać wiązanie z ekspanderem:

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

zestaw IsExpanded za pierwszy prawdziwy

+0

Ok to działało, zastanawiam się, czy można to zrobić tylko w kodzie XAML. – alexn234

2

można spróbować dodać inną właściwość bool do widoku modelu domyślnie prawda, ale przejście na Fałsz po pierwszym użyciu. I związaj w tym celu właściwość IsExpanded Expander z trybem OneTime.

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

Xaml byłoby tak:

<Expander IsExpanded="{Binding DataContext.IsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Mode=OneTime}"> 
+0

to również dobre podejście, spróbuję – alexn234