Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Individual DataGrid Row Visibility

I have a WPF DataGrid bound to a collection of Entity Framework objects that's inside a parent EF object. Something along the lines of:

<DataGrid ItemsSource="{Binding SelectedCustomer.Orders}" />

Now when I want to "delete" an Order, I don't want to actually delete it from the data source, I simply want to set its IsDeleted property to true so the data is retained.

My question is: how can I get my DataGrid to skip a row if it's IsDeleted property is true? I would really like to use binding and not codebehind. Something like this would be wonderful:

<DataGrid ItemsSource="{Binding SelectedCustomer.Orders}" RowVisibilityPath="IsDeleted" />

Kind of along the lines of DisplayMemberPath. I realize I would need to convert the state of IsDeleted, but that's a different topic.

Any ideas?

like image 594
5e360a6f-ec03-444a-bac4-a700fc Avatar asked May 08 '11 15:05

5e360a6f-ec03-444a-bac4-a700fc


3 Answers

Aside from using a CollectionView as mentioned you can do this via the RowStyle:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsDeleted}" Value="True">
                <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>
like image 34
H.B. Avatar answered Nov 16 '22 23:11

H.B.


<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
       <Setter Property="Visibility" Value="{Binding IsDeleted, Converter={StaticResource BoolToVisibility}}"/>                                     
    </Style>
</DataGrid.RowStyle>
like image 53
denis morozov Avatar answered Nov 16 '22 21:11

denis morozov


You can use a CollectionView to filter your data.

like image 45
Ben Avatar answered Nov 16 '22 23:11

Ben