Search code examples
c#wpfdatagridhide

Hide DataGrid row with specific Value C# WPF


I have an xml file with and they got an Attribute "Active = true". If i delete a Customer, it sets "active" to false, but the customer should still be in my xml file. I simply want to hide the DataGrid Column where the row "active" is false. So every customer with "active = false" should not be displayed in my Data Grid. I hope you understand what im trying to do :P

I thought about something like this:

private void HideCustomer()
        {
            if (active == false)
            {
                DataGrid.HideRow ???? // So if the customer has this attribute set to "false" the row 
            }                         // should be hidden in the DataGrid
        }

Solution

  • You could define an RowStyle with a DataTrigger in the XAML markup:

    <DataGrid ...>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsActive}" Value="False">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
    

    This requires that IsActive is a public property. You should also implement INotifyPropertyChanged to raise change notifications.