Category don’t have child how can it be removed.

Grids for WPF Forum

Posted 10 years ago by Balaram Barange
Version: 4.5.0487
Avatar
We have generated Property Control using PropertyGridPropertyItem and this.PropertyControl.Properties.Add(PropertyGridPropertyItem)[Code Behind].

As per our logic we need to make some properties' visibility to Visibility.Collaped and sometime they all belongs to same category, so it will show only category name that we need to remove.

How can I remove category if there is no child at all?

Comments (9)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Balaram,

Off the top of my head, I can't think of a nice clean and automatic way of handling that. The problem is you now have the visibility of the Category based on the visibility of it's children, but there are no properties on the Category that you can trigger off of (e.g. there is no VisibleChildCount).

The only way I can think of to accomplish this is to listen for the IsVisibleChanged event for the PropertyGridDataAccessorItem on the PropertyGrid. When you see that
a property is shown or hidden, then you would search up the visual tree for the associated category. When you have the PropertyGridDataAccessorItem for the category, then you would update it's visibility based on the items in the Accessors collection (e.g. if there are any visible items then the category would be visible).


Actipro Software Support

Posted 10 years ago by Balaram Barange
Avatar
Hi Support Team,

Thanks for the quick response.

As I don't use any category item/property item class/object to generate category and we use following code to create and add property to PropertyGrid control.

So when we set "Category of PropertyGridPropertyItem" to some value("General Properties"), it will create category of that value("General Properties") and shown on PropertyControl. And when we get count using this.propGrid.Properties.Count, it returns 1.

So, please suggest how can I get reference of category?("When you have the PropertyGridDataAccessorItem for the category, then you would update it's visibility based on the items in the Accessors collection")
PropertyGridPropertyItem item = new PropertyGridPropertyItem();
            item.DisplayName = "Combo Property";
            item.Category = "General Properties"; // It automatically creates category.
            item.Description = "Description of combobox type";
...
this.propGrid.Properties.Add(item);
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
In your handler for the IsVisibleChanged event, the sender should be the PropertGridDataAccessorItem for the property that you made visible or collapsed. Then you can use VisualTreeHelperExtended to get the PropertyGridDataAccessorItem ancestor, which should be the category.

Once you have that, you can hide the PropertyGridDataAccessorItem for the category by setting the Visibility property.

You may need to attach to the IsVisibleChanged event like:
<propgrid:PropertyGrid ... propgrid:PropertyDataAccessorItem.IsVisibleChanged="YourHandler" ...
The category would be available at the data layer in the PropertyGrid.Items collection, but it does not have a Visibility property. So you would need to hide it at the UI layer, which is what is described above.

Hope this makes sense.


Actipro Software Support

Posted 10 years ago by Balaram Barange
Avatar
As per our current implementation of Property Grid control I have prepare sample application but the event is not fired. Please look into below example and suggest how to hide category when item's visibility set to collapsed.

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:propgrid="http://schemas.actiprosoftware.com/winfx/xaml/propgrid"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="452" Width="265">
    <Grid>
        <propgrid:PropertyGrid Name="propGrid" IsCategorized="True" MiscCategoryTitle="General" Margin="0,0,0,53" ModifiedPropertyDisplayMode="None">
            <propgrid:PropertyGrid.Resources>
                <Style TargetType="{x:Type propgrid:PropertyGridDataAccessorItem}">
                    <Setter Property="Visibility" Value="{Binding Visibility}" />
                </Style>
            </propgrid:PropertyGrid.Resources>
        </propgrid:PropertyGrid>
        <Button Height="23" Margin="12,0,0,29" Name="button1" VerticalAlignment="Bottom" Click="button1_Click" HorizontalAlignment="Left" Width="75">Button 1</Button>
    </Grid>
</Window>

private void Init1()
        {
            PropertyGridPropertyItem item = new PropertyGridPropertyItem();
            item.Category = "General Properties";
            item.DisplayName = "Simple";
            item.Description = "Test";
            item.Value = "MyValue";
            item.IsVisibleChanged +=new DependencyPropertyChangedEventHandler(item_IsVisibleChanged);
            propGrid.Properties.Add(item as IPropertyDataAccessor);
        }

        void item_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            int i = 0; // Not executed even if visiblity changed by pressing the button.
        }

private void button1_Click(object sender, RoutedEventArgs e)
        {
            (PropertyGridPropertyItem)propGrid.Properties[0]).Visibility = Visibility.Collapsed;

        }
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
You are trying to handle the IsVisibleChanged event of the PropertyGridPropertyItem. This item is never visible, since it is only used in the data layer. Therefore, the IsVisible property will always be false.

The PropertyGridPropertyItem is presented by a PropertyGridDataAccessorItem, which will be visible. The implicit Style you include is binding the Visibility property of the PropertyGridPropertyItem to the Visibility property of the PropertyGridDataAccessorItem. So effectively when you set the Visibility of the PropertyGridPropertyItem to Collapsed, the Visibility property of the PropertyGridDataAccessorItem is updated *and* it's IsVisible property is set to false. Therefore, you need to handle the IsVisibleChanged events coming from PropertyGridDataAccessorItem (not PropertyGridPropertyItem).

To attach a generic handler that catches the IsVisibleChanged event for any PropertyGridDataAccessorItem, then you need to attach the event handler as I described earlier. For the code you posted, you would need to add:
<propgrid:PropertyGrid ... propgrid:PropertyDataAccessorItem.IsVisibleChanged="item_IsVisibleChanged" ...


Actipro Software Support

Posted 10 years ago by Balaram Barange
Avatar
I have tried to add above portion like following, but it could not resolve the "PropertyDataAccessorItem" Symbol.

<propgrid:PropertyGrid Name="propGrid" propgrid:PropertyGrid.PropertyDataAccessorItem.IsVisibleChanged="item_IsVisibleChanged" IsCategorized="True" MiscCategoryTitle="General Properties" Margin="0,0,0,53" ModifiedPropertyDisplayMode="None" IsVisibleChanged="item_IsVisibleChanged" >
Let me know any namespace or refernce missing. The previous post contains whole code.
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Sorry, it's PropertyGridDataAccessorItem (as described in previous text).


Actipro Software Support

Posted 10 years ago by Balaram Barange
Avatar
Can you please reply with our code?

As I am not able to set
propgrid:PropertyGridDataAccessorItem.IsVisibleChanged="propGrid_IsVisibleChanged"
line in this
<propgrid:PropertyGrid Name="propGrid" propgrid:PropertyGridDataAccessorItem.IsVisibleChanged="propGrid_IsVisibleChanged" IsCategorized="True" MiscCategoryTitle="General Properties" Margin="0,0,0,53" ModifiedPropertyDisplayMode="None"  >
.

And if I use same line inside the proeprty grid it displays one black category at top.
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
I apolygize, I thought IsVisibleChanged was a routed bubbling event, but it's not. So you won't be able to attached to it like I specified above.

Since there is no VisibilityChanged event, you would need to add one to a custom PropertryGridDataAccessorItem. The steps would be:

1. Create a class that derives from PropertyGridDataAccessorItem (e.g. MyPropertyGridDataAccessorItem)
2. Override IsItemItsOwnContainerOverride and GetContainerForItemOverride MyPropertyGridDataAccessorItem, to test for and return a MyPropertyGridDataAccessorItem.
3. Add a routed bubbling event (e.g. MyEvent).
4. Attach to the IsVisibleChanged event in the instance constructor.
5. Raise the event from #3 in the handler for #4.

6. Create a class that derives from PropertyGrid (e.g. MyPropertyGrid).
7. Override IsItemItsOwnContainerOverride and GetContainerForItemOverride in MyPropertyGrid, to test for and return a MyPropertyGridDataAccessorItem.

Once you have you can attach a handler for the event in #3 as previously described. Alternatively, you would handle it in your custom PropertyGrid (from #6) to make it more encapsulated.

If you have any problems, please send a small sample project to our support address.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) 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.