Hide properties in different categories when using CategoryEditor XAML

Grids for WPF Forum

Posted 2 years ago by Procam
Version: 22.1.2
Avatar

Hello,

I want to hide properties in different categories according to some change outside properry grid. The XAML is as below (the model contains much more properties but for simplicity, I use 3 properties: Name, Description and Gap in 2 categories):

        <grids:PropertyGrid x:Name="propertiesGrid" Margin="0" Grid.Row="1" HorizontalContentAlignment="Stretch" IsSummaryVisible="False"
                            IsFilterActive="True" CanClearDataObjectOnUnload="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0"
                            DataObjects="{Binding SelectedItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
           
            <grids:PropertyGrid.CategoryEditors>

                <grids:CategoryEditor Category="Basic information" DisplayName="Basic information" x:Name="basic1">
                    <grids:CategoryEditor.Properties>
                        <grids:CategoryEditorProperty PropertyName="Name" />
                        <grids:CategoryEditorProperty PropertyName="Description" />
                    </grids:CategoryEditor.Properties>
                    <grids:CategoryEditor.EditorTemplate>
                        <DataTemplate>
                            <Grid Grid.IsSharedSizeScope="True">
                                <ItemsControl  Margin="5" ItemsPanel="{StaticResource FluidMovePanelTemplate}" Width="{Binding ElementName=main, Path=ActualWidth, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
                                    <GridTextBox Label="{Binding Children[Name].Name}" SelectedValue="{Binding Children[Name].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                    <GridTextBox Label="{Binding Children[Description].Name}" SelectedValue="{Binding Children[Description].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                </ItemsControl>
                            </Grid>
                        </DataTemplate>
                    </grids:CategoryEditor.EditorTemplate>
                </grids:CategoryEditor>

                <grids:CategoryEditor Category="Basic information 2" DisplayName="Basic information 2" x:Name="basic2">
                    <grids:CategoryEditor.Properties>
                        <grids:CategoryEditorProperty PropertyName="Gap" />
                    </grids:CategoryEditor.Properties>
                    <grids:CategoryEditor.EditorTemplate>
                        <DataTemplate>
                            <Grid Grid.IsSharedSizeScope="True">
                                <ItemsControl  Margin="5" ItemsPanel="{StaticResource FluidMovePanelTemplate}" Width="{Binding ElementName=main, Path=ActualWidth, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
                                    <GridTextBox Label="{Binding Children[Gap].Name}" SelectedValue="{Binding Children[Gap].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                </ItemsControl>
                            </Grid>
                        </DataTemplate>
                    </grids:CategoryEditor.EditorTemplate>
                </grids:CategoryEditor>

            </grids:PropertyGrid.CategoryEditors>
            
            <!--<grids:PropertyGrid.DataFactory>
                <sample:CustomDataFactory />
            </grids:PropertyGrid.DataFactory>-->

            <!--<grids:PropertyGrid.ItemContainerStyleSelector>
                <grids:PropertyGridItemStyleSelector>
                    <grids:PropertyGridItemStyleSelector.PropertyStyle>
                        <Style TargetType="grids:PropertyGridItem">
                            <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}" />
                        </Style>
                    </grids:PropertyGridItemStyleSelector.PropertyStyle>
                </grids:PropertyGridItemStyleSelector>
            </grids:PropertyGrid.ItemContainerStyleSelector>-->

        </grids:PropertyGrid>

My first attempt was to use your sample "PropertyGridDynamicProperties" using CustomDataFactory (commented in XAML) and CustomPropertyModel, but seems it works when not using CategoryEditors as in XAML.

Another attempt was to use a custom filter as below:

        private bool CustomFilterPredicate(object model)
        {
            logger.Info($"=====> {model.GetType()}");
            int count = 0;

            if (model is ICategoryEditorModel cem)
            {
                count++;
                logger.Info($"=> cem {cem.DisplayName} - {cem.Name}");
            }
            if (model is ICategoryModel cm)
            {
                count++;
                logger.Info($"=> cm {cm.DisplayName} - {cm.Name}");
            }
            if (model is IPropertyModel pm/* && pm.Category != "Misc"*/)
            {
                count++;
                logger.Info($"=> pm {pm.DisplayName} - {pm.Name}");
            }
            if (count == 0)
            {
                // never hit => no another type
            }
            return (model is ICategoryEditorModel propertyModel)/* && (propertyModel.DisplayName != propertiesGrid.MiscCategoryName)*/;
        }

When I logged that, I realize that the defined properties in the XAML (Name, Description and Gap) never come, but when I remove the stuff for:

<grids:CategoryEditorProperty PropertyName="Description" />

so this property appears in the log. It means I am not able to hide the desired properties.

How can I hide properties when using CategoryEditors?

I like the way using the IsVisible property in your CustomPropertyModel but it did not work on my first attempt.

Comments (1)

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Yes a custom filter (a core TreeListBox mechanism) on the PropertyGrid is only looking at the "rows" in the PropertyGrid, which would be one of the three interface types you track counts for in your CustomFilterPredicate.

For your question, if you are trying to hide individual properties within the category editor, each of those items in the Children collection that you bind to with be IPropertyModel objects.  Since you are in control of the UI within the category editor, can't you bind to the Visibility property on the related UI control within the category editor template for the properties that can hide?


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.