2017-10-03 60 views
0

コンテンツを折りたたむことなく、XAMLのコントロールGroupBoxの枠線を折りたたんで(つまり、VMのプロパティにバインドする)ことはできますか?コンテンツを折りたたまないでGroupBoxの枠を折りたたむ

境界線を削除したいだけでなく、BorderThicknessを0に、Headerを空の文字列に設定することで実現できます。私はまた、GroupBoxのコンテンツを国境のどこに伸ばしたいのですか?

<DataTemplate DataType="{x:Type config:ElementGroup}"> 
    <DataTemplate.Resources> 
     <Style TargetType="{x:Type GroupBox}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=HideBorder}" Value="True"> 
        <Setter Property="Visibility" Value="Collapsed" /> 
       </DataTrigger> 
      </Style.Triggers> 
      <Setter Property="Foreground" Value="{StaticResource TextColor}" /> 
      <Setter Property="Header" Value="{Binding Path=ItemLabel}" /> 
      <Setter Property="Margin" Value="5,0,5,0" /> 
     </Style> 
    </DataTemplate.Resources> 
    <GroupBox> 
     <ItemsControl ItemsSource="{Binding Path=ElementList}" Visibility="Visible"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Columns="{Binding Path=Columns}" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
     </ItemsControl> 
    </GroupBox> 
</DataTemplate> 

答えて

1

、またそのContentを崩壊します。

あなたはこれをしたくない場合は、GroupBoxが折りたたまれますときに表示される別のItemsControl定義することができます。

<DataTemplate DataType="{x:Type config:ElementGroup}"> 
    <DataTemplate.Resources> 
     <Style TargetType="{x:Type GroupBox}"> 
      <Setter Property="Foreground" Value="{StaticResource TextColor}" /> 
      <Setter Property="Header" Value="{Binding Path=ItemLabel}" /> 
      <Setter Property="Margin" Value="5,0,5,0" /> 
     </Style> 
    </DataTemplate.Resources> 
    <Grid> 
     <GroupBox x:Name="gb"> 
      <ItemsControl ItemsSource="{Binding Path=ElementList}" Visibility="Visible"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <UniformGrid Columns="{Binding Path=Columns}" /> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </GroupBox> 
     <ItemsControl x:Name="ic" ItemsSource="{Binding Path=ElementList}" Visibility="Collapsed"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Columns="{Binding Path=Columns}" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
     </ItemsControl> 
    </Grid> 
    <DataTemplate.Triggers> 
     <DataTrigger Binding="{Binding Path=HideBorder}" Value="True"> 
      <Setter TargetName="gb" Property="Visibility" Value="Collapsed" /> 
      <Setter TargetName="ic" Property="Visibility" Value="Visible" /> 
     </DataTrigger> 
    </DataTemplate.Triggers> 
</DataTemplate> 
+0

パフォーマンスに関する[codeDom](https://stackoverflow.com/users/5306861/codedom)のアプローチと比較して、このアプローチに関する考えはありますか? 'ItemsControl'がバインドする' ElementList'は、1秒間に数回更新する要素を含んでいます。 – Oystein

+0

あなたはまだ非常に同じオブジェクトで非常に同じコレクションにバインドしていますか?同じソースコレクションに2回バインドしても、コレクション自体のコピーやその中のオブジェクトは作成されません。 – mm8

+0

重複したItemsControlはどうですか? VM内の通知をオフにすると、折りたたまれたItemsControlも更新され、折りたたまれていても再描画されるため、追加のリソースが消費されますか? – Oystein

2
これにあなたの GroupBoxスタイルを変更

すなわち Hiddenまたは Collapseにその Visibilityプロパティを設定 GroupBoxを、折りたたみ
<Style TargetType="{x:Type GroupBox}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=HideBorder}" Value="True"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="GroupBox"> 
          <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" 
             Content="{TemplateBinding Content}" 
             ContentStringFormat="{TemplateBinding ContentStringFormat}" 
             Margin="{TemplateBinding Padding}" 
             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </DataTrigger> 
     </Style.Triggers> 
     <Setter Property="Foreground" Value="{StaticResource TextColor}" /> 
     <Setter Property="Header" Value="{Binding Path=ItemLabel}" /> 
     <Setter Property="Margin" Value="5,0,5,0" /> 
    </Style> 
+0

を、あなたは作業例を提供することはできますか?私はあなたの提案を試みましたが、それを働かせることはできません。 'GroupBox'はただ一つの' ItemsControl'を含んでいます。しかし、明示的に 'ItemsControl'' Visibility'プロパティを' Visible'に設定したとしても、 'GroupBox''Visibility'を' Hidden'または 'Collapsed'に設定すると、' GroupBox'と 'ItemsControl'内はすべて消えてしまいます。 – Oystein

+0

@Oystein私は実際の例を追加しました。 – codeDom

+0

素晴らしいです。しかし、なぜグリッド値ですか?どのグリッドですか?それがなければまったく同じように動作します。 – Oystein

関連する問題