Master-Detail hide expander on empty details section

Master-Detail hide expander on empty details section

I’m binding to a BindingList to a class that has a child BindingList for the details that show up in my Master-Detail grid.

Based on records in the user’s DB, some of master records don’t have any child records. I’ve searched high & low trying to figure out a way to hide or disable the expander when the master has no child rows.

I’ve provided my own Collapse/ExpandGroupGlyph (see XAML below) – it’d be great if I could somehow add binding to the Visibility property that somehow bound to the current row has child items or not.

I tried the following: – but there doesn’t seem to be a HasChildItems of the DataRow.

I tried pasting in the XAML using the code button, it won’t let me submit.

XAML code with the less than/greater than chars replaced with html codes:

                            <!--  Not sure why the wonkiness, but getting the icon centered required a weird margin + wrapper grid.  -->
                            <Grid x:Name="grd"
                                    <DropShadowEffect BlurRadius="0"
                                                      ShadowDepth="1" />
                                <Border x:Name="brd"
                                                <DataTrigger Binding="{Binding ElementName=grd,
                                                    <Setter Property="Border.Background"
                                                            Value="{DynamicResource appIconColor}" />
                                    <Path Width="10"
                                          Data="M0,5 H10 M5,5 V10Z"
                                          StrokeThickness="2" />

I made a little progress on this one. I added a readonly property called HasChildRows to master item class that returns true if the count of the child binding list > 0.

I then was able to use FindAncestor and bind to the parent DataRow.DataContext.HasChildRows. This works, technically.

                                  Visibility="{Binding Mode=OneWay,
                                                       RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                      AncestorType={x:Type g:DataRow}},
                                                       Converter={StaticResource BooleanToVisibilityConverter}}"

^ the problem with the above is at runtime, Visual Studio has a silent binding error for every new row that gets loaded:

System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=’Xceed.Wpf.DataGrid.DataRow’, AncestorLevel=’1”. BindingExpression:Path=DataContext.HasChildRows;

^ So it looks like binding is failing (at least at first) then succeeding. Is there a better way to bind so it isn’t failing at first. I’m just worried about performance if there is a bunch of rows from the master table are returned in query and every single row is going to initially fail it’s binding.