2016-12-19 4 views
0

リストボックスに「Expander」の各グループアイテムを囲んだグループアイテムのリストがあり、常に最初のインスタンスが折りたたまれています。項目がリストボックスに特定のグループに追加されると、その時点でそのグループのそれぞれの展開が展開されます(IsExpanded = true)。WPF ListBoxグループアイテムがそれぞれのグループに追加されている場合、GroupItemエキスパンダーを展開する必要があります。

以下は私が今まで試したスタイルです。ここに何もないのですか?

<Style x:Key="GroupContainerStyleA" TargetType="{x:Type GroupItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate> 
         <Expander IsExpanded="False" Style="{StaticResource ExpanderStyle}"> 
          <Expander.Header> 
           <Border> 
            <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"/> 
           </Border> 
          </Expander.Header> 
          <Border Background="White" Margin="1.5,0,1.5,1.5" BorderBrush="{StaticResource SideButtonBackgroundBrushKey}" BorderThickness="0.5"> 
           <ItemsPresenter Margin="5,0,0,5"/> 
          </Border> 
         </Expander> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

答えて

1

展開するには、ExpanderのIsExpandedプロパティをtrueに設定する必要があります。あなたは、例えば、グループ内の項目のて、CollectionChangedイベントをサブスクライブすることによってこれを行うことができます:

private void Expander_Loaded(object sender, RoutedEventArgs e) 
{ 
    Expander expander = sender as Expander; 
    CollectionViewGroup cvs = expander.DataContext as CollectionViewGroup; 
    if (cvs != null) 
    { 
     INotifyCollectionChanged coll = cvs.Items as INotifyCollectionChanged; 
     if (coll != null) 
     { 
      WeakEventManager<INotifyCollectionChanged, NotifyCollectionChangedEventArgs>.AddHandler(coll, "CollectionChanged", 
       (ss, ee) => expander.IsExpanded = true); 
     } 
    } 

} 

<Style TargetType="{x:Type GroupItem}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Expander IsExpanded="False" Loaded="Expander_Loaded"> 
        <Expander.Header> 
         <Border> 
          <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"/> 
         </Border> 
        </Expander.Header> 
        <Border Background="White" Margin="1.5,0,1.5,1.5" BorderBrush="Black" BorderThickness="0.5"> 
         <ItemsPresenter Margin="5,0,0,5"/> 
        </Border> 
       </Expander> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

それとも、あなたのいくつかのソースプロパティにパンダのisExpandedとしてプロパティに結合することができます。詳細については、次の質問を参照してください。

wpf datagrid automatically expand first group

+0

私はそれは.NET Frameworkの4.5+における第WindowsBaseアセンブリ内System.Windows名前空間にありますWeakEventManager nag

+0

の名前空間を見つけることができません。 System.Windowsを使用して追加します。あなたのコードファイルの先頭に。 .NETの弱いイベントパターンの詳細については、次の記事を参照してください。https://www.codeproject.com/articles/738109/the-net-weak-event-pattern-in-csharp他のオプションは、直接参照を使用してイベントハンドラをフックすることです:coll.CollectionChanged + =(ss、ee)=> expander.IsExpanded = true; – mm8

+0

ああ、私のターゲットバージョンは4.5です。今何ができますか? – nag

関連する問題