Search code examples
c#xamlxamdatagrid

Expand all method expands all records apart from the first one


I've added this button to expand all the records:

    <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="right">
        <StackPanel.Style>
            <Style TargetType="StackPanel">
                <Setter Property="Visibility" Value="Visible"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ActiveTreeNode}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ActiveTreeNode}">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>
        <Button Content="Expand All" Click="ExpandAllRateParts_Click"/>
    </StackPanel>

and in the xaml.cs:

private void ExpandAllRateParts_Click(object sender, RoutedEventArgs e)
{
    var ratesGrid = RatesGrid;

    ratesGrid.UpdateLayout();
    ratesGrid.Records.ExpandAll(true);

}

The issue I'm having is that on the very first click, is that it will expand all the records apart from the first one. When I click it again, the first one will expand. I'm just not sure why this would be happening or what I should be checking?


Solution

  • The issue here was nothing with this part at all:

    private void ExpandAllRateParts_Click(object sender, RoutedEventArgs e)
    {
        var ratesGrid = RatesGrid;
    
        ratesGrid.UpdateLayout();
        ratesGrid.Records.ExpandAll(true);
    
    }
    

    it actually worked and could even be simplified as:

    private void ExpandAllRateParts_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        RatesGrid.Records.ExpandAll(true);
    }
    

    The issue was happening due to a method within the view model that was incorrectly getting called twice, this caused the grid to refresh which is why that odd behaviour was occurring