Disable/Hide Category Expander based on Databinding

Grids for WPF Forum

Posted 13 years ago by Pat Maneely - Software Engineering Manager, Cobham Aerospace
Avatar
I'm trying to disable an expanding selectively based on a property in my View Model.

The Data Context of the XAML is bound to the VM. The VM has property called Count that indicates the number of parts in the system. I need to Collapse and Hide (or Disable) one or more of the Categories in the PropertyGrid.

How Do I do this? I was trying to use a MultiDataTrigger, but I can't figure out how to link to the Datacontext.

Here's the XAML...


<Style TargetType="{x:Type propgrid:PropertyGridDataAccessorItem}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DataAccessorType, RelativeSource={RelativeSource Self}}" Value="Category" />
<Condition Binding="{Binding DisplayName, RelativeSource={RelativeSource Self}}" Value="Misc" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Hidden" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DataAccessorType, RelativeSource={RelativeSource Self}}" Value="Category" />
<Condition Binding="{Binding DisplayName, RelativeSource={RelativeSource Self}}" Value="System #1" />
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RpWin:SystemData}}, Path=Count}" Value="0" />
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Hidden" />
</MultiDataTrigger>
</Style.Triggers>
</Style>

<wizard:Wizard x:Name="configSetupWizard" WindowTitleBehavior="PageTitle" WindowTitleBaseText="Configuration Wizard"
PageSequenceType="Stack" BackwardProgressTransitionDuration="0:0:0.5" ForwardProgressTransitionDuration="0:0:0.5"
HelpButtonVisible="False" IsWindowDialogResultUpdatingEnabled="True">
<wizard:Wizard.TransitionSelector>
<shared:MultiTransitionSelector>

<shared:BarWipeTransition />
</shared:MultiTransitionSelector>
</wizard:Wizard.TransitionSelector>
<wizard:WizardPage x:Name="msmFSPPage"
Caption="System Configuration Parameters"
Description="This page contains the Parameters necessary to configuration System"
Title="System Configuration Parameters Page"
HelpButtonVisible="False"
NextButtonEnabled="{Binding Path=SysData.CapableSystem}"
FinishButtonEnabled="{Binding Path=FlexCommSysData.P25CapableSystem, Converter={StaticResource BoolInverterConverter}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0"
MinHeight="{Binding RelativeSource={RelativeSource Self}, Path=Content.MinHeight}"
Content="Configuration Name" />
<TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
MaxLines="1" Margin="-100,0,0,0" HorizontalAlignment="Stretch" Width="Auto"
Text="{Binding Path=SysData.Name}"/>
<propgrid:PropertyGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
IsSummaryVisible="True"
SelectedObject="{Binding Path=SysData}">
<propgrid:PropertyGrid.DataFactory>
<propgrid:TypeDescriptorFactory />
</propgrid:PropertyGrid.DataFactory>
</propgrid:PropertyGrid>
</Grid>
</wizard:WizardPage>



The VM has a Property called SysData. In SysData is a Property called Count. When 'Count' = 0 I need to disable/Hide the 'System #1' Category Expander so that the user can't select any children properties.

Comments (5)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Pat,

I think you'd want to use this in your MultiDataTrigger:
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type propgrid:PropertyGridDataAccessorItem}}, Path=DataContext.Count}" Value="0" />


Actipro Software Support

Posted 13 years ago by Pat Maneely - Software Engineering Manager, Cobham Aerospace
Avatar
No Joy :(

If gives me the following error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.PropertyGridDataAccessorItem', AncestorLevel='1''. BindingExpression:Path=DataContext.Count; DataItem=null; target element is 'PropertyGridDataAccessorItem' (Name=''); target property is 'NoTarget' (type 'Object')
Posted 13 years ago by Pat Maneely - Software Engineering Manager, Cobham Aerospace
Avatar
So I guess my biggest question is how do I get to the PropertyGrid's Datacontext from a Category or a Property DataAccessorItem? If I could figure that out I could create a MultiDataTrigger that could bind to the ViewModel's Count property.

I've got to believe I'm not understanding something really simple because it seems incredibly hard to control properties and categories from properties in the VM bound to the PropertyGrid.
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Pat,

Sorry, that should have been:
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type propgrid:PropertyGrid}}, Path=DataContext.Count}" Value="0" />
Basically, that will find the containing PropertyGrid and access it's DataContext. If you are just trying to hide categories that have no properties in them, then this would work also:
<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
        <Condition Binding="{Binding DataAccessorType, RelativeSource={RelativeSource Self}}" Value="Category" /> 
        <Condition Binding="{Binding DisplayName, RelativeSource={RelativeSource Self}}" Value="System #1" /> 
        <Condition Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="False" /> 
    </MultiDataTrigger.Conditions> 
    <Setter Property="Visibility" Value="Hidden" /> 
</MultiDataTrigger>
Based on the information you've provided it's difficult to tell what is the best approach, or if there is another better way to do it. If you can put together a small sample project that reproduces your issue and email it over then we can take a closer look. Be sure to remove any executables or change the extension of the zip file to ensure it gets past our email filters.


Actipro Software Support

Posted 13 years ago by Pat Maneely - Software Engineering Manager, Cobham Aerospace
Avatar
Bingo!! Thanks a lot!
The latest build of this product (v24.1.3) was released 28 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.