2013-01-14 13 views
5

genişletin. Koleksiyonu doldurduğumda, ilk grubun genişletilmiş olarak nasıl görüntüleneceğini, nasıl kodlanacağını wpf (codebehind veya mvvm) olarak istiyorum? MVVM denetleyicisi wpf datagrid otomatik Bir grupla bir ListCollectionView bağlı ItemSource bir veri kılavuzunu var ilk grubu

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

:

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

kod yürütülmesi datagrid 1 öğe seçilir doldurulur ama grup daraltılmıştır.

+0

Bu çok belirsiz olduğundan,? – akjoshi

+0

ben selectedItem özelliğini ayarlamak için – alexn234

cevap

11

dersinize IsExpanded özellik ekleyin ve eklemek bağlayıcı Expander'a:

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

Seti IsExpanded ilk için de geçerlidir

+0

koleksiyon görünümü ayarlandıktan sonra çalıştım, bu sadece xaml kodu yapılabilirdi merak ediyorum. – alexn234

2

için size Görünüm Modeli başka bool özelliğini eklemek deneyebilirsiniz gerçek ama geçiş için varsayılan İlk kez kullanıldığında yanlış. Ve OneGime modu ile Expander'in IsExpanded özelliğini buna bağlayın.

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

Xaml böyle olacaktır: Şimdiye kadar ne denedim

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

bu da iyi bir yaklaşım, onu deneyecek – alexn234

İlgili konular