2013-01-14 60 views
5

私は、ひとつのグループを持つListCollectionViewにバインドされたitemsourceを持つDataGridを持っています。 私はコレクションを埋めるとき、私は最初のグループを自動的に拡大して見たい、wpf(codebehindまたはmvvm)でそれをコード化する方法は? MVVMコントローラで wpf datagridが最初のグループを自動的に展開します

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

コードを実行し、データグリッドは、第1項目が選択され充填されるが、グループが縮小されます。

+0

これは、あなたがこれまでにしようとしているものを、あまりにも漠然としているのですか? – akjoshi

+0

コレクションビューを設定した後にSelectedItemプロパティを設定しようとしました。 – alexn234

答えて

11

は、あなたのクラスにisExpandedとしてプロパティを追加し、パンダに結合追加:あなたはあなたのビューモデルに別のブール値プロパティを追加しようとすることができ、真の

+0

これはうまくいきました。xamlコードでのみ実行できるかどうか疑問です。 – alexn234

2

への最初のために

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

設定isExpandedとしてはtrueにデフォルト設定だけに切り替え初回使用時はfalse。また、ExpanderのIsExpandedプロパティをOneTimeモードでバインドします。

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

XAMLはそのようになります:

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

これはまた良いアプローチです、試してみましょう – alexn234

関連する問題