2016-12-19 13 views
1

私はこのコードを使用して、特定のプロパティを隠す:WPF:空のカテゴリを非表示に

propertyItem.Visibility = Visibility.Collapsed; 

は、完全なカテゴリを非表示にすることが可能ですか?これで問題は解決しました:

if (propertyItem.Category == "MyCategory") 
{ 
    propertyItem.Visibility = Visibility.Collapsed; 
} 

カテゴリのすべての項目を非表示にすると、その項目のヘッダーが常に表示されます。カテゴリのヘッダーを隠す方法はありますか?

答えて

1

あなたの目標はそれほど簡単ではありませんが、到達することは不可能ではありません。あなたが見ることができるように

<local:ItemsVisibleConverter x:Key="ItemsVisibleConverter" /> 

<Style x:Key="CustomPropertyItemGroupContainerStyle" TargetType="{x:Type GroupItem}"> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Border> 
        <Expander Style="{StaticResource ExpanderStyle}" Header="{Binding Name}" IsExpanded="True"> 
         <ItemsPresenter /> 
        </Expander> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

    <Style.Triggers> 
     <DataTrigger Binding="{Binding Path=Items, Converter={StaticResource ItemsVisibleConverter}}" Value="False"> 
      <Setter Property="Visibility" Value="Collapsed" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

が、それは簡単なコンバータ使用しています:私たちが目に見える性質なしカテゴリ隠さ作るために私たちのスタイルを作成するために必要なすべての まず

public class ItemsVisibleConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     IEnumerable itemCollection = value as IEnumerable; 
     if (itemCollection != null) 
     { 
      foreach (PropertyItem propertyItem in itemCollection) 
      { 
       if (propertyItem.Visibility == Visibility.Visible) 
       { 
        return true; 
       } 
      } 
     } 

     return false; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

といくつかの他のリソースを(I )ILSpyを使用して標準的なものからそれらをコピー:

<SolidColorBrush x:Key="GlyphBrush" Color="#FF31347C" /> 

<ControlTemplate x:Key="ExpanderToggleButton" TargetType="{x:Type ToggleButton}"> 
    <Grid> 
     <Rectangle Name="Rectangle" Margin="0,0,0,0" Fill="#00FFFFFF" /> 
     <Path Name="Up_Arrow" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="{StaticResource GlyphBrush}" Data="M0,0L4,4 8,0z" RenderTransformOrigin="0.5,0.5"> 
      <Path.RenderTransform> 
       <TransformGroup> 
        <ScaleTransform ScaleX="1" ScaleY="1" /> 
        <SkewTransform AngleX="0" AngleY="0" /> 
        <RotateTransform Angle="-90" /> 
        <TranslateTransform X="0" Y="0" /> 
       </TransformGroup> 
      </Path.RenderTransform> 
     </Path> 
     <Path Name="Down_Arrow" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="{StaticResource GlyphBrush}" Data="M0,4L4,0 8,4z" RenderTransformOrigin="0.5,0.5"> 
      <Path.RenderTransform> 
       <TransformGroup> 
        <ScaleTransform ScaleX="1" ScaleY="1" /> 
        <SkewTransform AngleX="0" AngleY="0" /> 
        <RotateTransform Angle="135" /> 
        <TranslateTransform X="0" Y="0" /> 
       </TransformGroup> 
      </Path.RenderTransform> 
     </Path> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="ToggleButton.IsChecked" Value="true"> 
      <Setter TargetName="Down_Arrow" Property="UIElement.Visibility" Value="Visible" /> 
      <Setter TargetName="Up_Arrow" Property="UIElement.Visibility" Value="Collapsed" /> 
      <Setter TargetName="Down_Arrow" Property="UIElement.OpacityMask" Value="#FF000000" /> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

<Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}"> 
    <Setter Property="Control.Padding" Value="0" /> 
    <Setter Property="Control.Background" Value="#FFF0F0F0" /> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Expander}"> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Name="ContentRow" Height="*" /> 
        </Grid.RowDefinitions> 
        <Border Name="Border" Background="{TemplateBinding Control.Background}" BorderBrush="#FFF0F0F0"> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="20" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 
          <ToggleButton Template="{StaticResource ExpanderToggleButton}" OverridesDefaultStyle="True" IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" /> 
          <ContentPresenter Grid.Column="1" Margin="1" RecognizesAccessKey="True" ContentSource="Header" TextElement.FontWeight="Bold" /> 
         </Grid> 
        </Border> 
        <Border Name="ExpandSite" Visibility="Collapsed" Grid.Row="1" Background="{x:Static SystemColors.ControlBrush}" Padding="10 0 0 0"> 
         <Border BorderThickness="0" Margin="0" Padding="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" Margin="{TemplateBinding Control.Padding}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" Focusable="False" /> 
         </Border> 
        </Border> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="Expander.IsExpanded" Value="True"> 
         <Setter TargetName="ExpandSite" Property="UIElement.Visibility" Value="Visible" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

は、今、私たちはトンで私たちのスタイルを設定するためのPropertyGridコントロールを拡張する必要があります彼はPropertyGridに含まPropertyItemsControl

public class PropertyGrid : Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid 
{ 
    public GroupStyle GroupStyle 
    { 
     get; 
     set; 
    } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     PropertyItemsControl propertyItemsControl = 
      Template.FindName("PART_PropertyItemsControl", this) as PropertyItemsControl; 
     propertyItemsControl.GroupStyle.Clear(); 
     propertyItemsControl.GroupStyle.Add(GroupStyle); 
    } 
} 

だからあなたのXAMLは、私はそれはあなたを助けることを願って

<local:PropertyGrid x:Name="pg"> 
    <local:PropertyGrid.GroupStyle> 
     <GroupStyle ContainerStyle="{StaticResource CustomPropertyItemGroupContainerStyle}" /> 
    </local:PropertyGrid.GroupStyle> 
</local:PropertyGrid> 

になります。

+0

偉大な答え、ありがとう。それは動作しますが、最初のレイヤーに対してのみ、ExpandableObjectsを持っている場合、サブカテゴリーはvalueConverterを使用しませんが、理由はありません。 – Suplanus

+0

あなたがあなたの状況の詳細を提供しなければならない、または多分あなたは別の質問@Suplanus投稿する必要があります。状況はそれほど明確ではない –

+0

を問題の状況は次のとおりです。 - 区分1 - Category1.1 - カテゴリ2 - Category2.1 - 項目 したがって、カテゴリ1とカテゴリ1.1を折りたたむ必要があります。 – Suplanus

関連する問題