Get data rows

How to get a list of rows in a DataGrid? Not related items, but DataGridRows .

I need to control the visibility of these rows, and it can only be managed as a DataGridRow , and not as a data object.

Thanks!

+6
source share
2 answers

You can get the string using ItemContainerGenerator . This should work -

 for (int i = 0; i < dataGrid.Items.Count; i++) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator .ContainerFromIndex(i); } 
+22
source

I recommend defining a style for the DataGridRow that will be tied to visibility, whether it should be displayed or not. Just repeating through the lines will not be enough, as I mentioned in @ RV1987's answer.

 <DataGrid> <DataGrid.Resources> <Style TargetType="DataGridRow"> <Setter Property="Visibility" Value="{Binding ...}" /> </Style> </DataGrid.Resources> </DataGrid> 

EDIT:

What you link depends on where you store information about whether to display the string. For example, if every data object in your linked collection has a bool ShouldBeDisplayed property, you have something like this:

 <DataGrid> <DataGrid.Resources> <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" /> <Style TargetType="DataGridRow"> <Setter Property="Visibility" Value="{Binding Path=ShouldBeDisplayed, Converter={StaticResource booleanToVisibilityConverter}}" /> </Style> </DataGrid.Resources> </DataGrid> 
0
source

Source: https://habr.com/ru/post/927105/


All Articles