1

我用这段代码隐藏了特定的属性:

propertyItem.Visibility = Visibility.Collapsed;

是否可以隐藏完整的类别?现在我的解决方法是:

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

如果我隐藏一个类别的所有项目,它的标题总是可见的。有没有办法隐藏类别的标题?

4

1 回答 1

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

和其他一些资源(我使用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控件来设置我们的样式在PropertyItemsControl包含的PropertyGrid

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>

我希望它可以帮助你。

于 2016-12-19T17:26:02.940 回答