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: http://forums.xceed.com/forums/topic/expander-visibility-issue/ – 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:

                    <g:TableView.ExpandGroupGlyph>
                        <DataTemplate>
                            <!--  Not sure why the wonkiness, but getting the icon centered required a weird margin + wrapper grid.  -->
                            <Grid x:Name="grd"
                                  Width="24"
                                  VerticalAlignment="Stretch"
                                  Background="Transparent">
                                <Grid.Effect>
                                    <DropShadowEffect BlurRadius="0"
                                                      RenderingBias="Performance"
                                                      ShadowDepth="1" />
                                </Grid.Effect>
                                <Border x:Name="brd"
                                        Width="16"
                                        Height="16"
                                        Margin="3,12"
                                        VerticalAlignment="Center"
                                        BorderBrush="LightGray"
                                        BorderThickness="1">
                                    <Border.Style>
                                        <Style>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding ElementName=grd,
                                                                               Path=IsMouseOver}"
                                                             Value="True">
                                                    <Setter Property="Border.Background"
                                                            Value="{DynamicResource appIconColor}" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Border.Style>
                                    <Path Width="10"
                                          Height="10"
                                          Margin="1"
                                          Data="M0,5 H10 M5,5 V10Z"
                                          Stroke="White"
                                          StrokeThickness="2" />
                                </Border>
                            </Grid>
                        </DataTemplate>
                    </g:TableView.ExpandGroupGlyph>
_____________________________________________________________________________

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}},
                                                       Path=DataContext.HasChildRows,
                                                       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.