2

How do I get a row in UWP? I have found solutions in WPF but UWP doesn't seem to have those options.

And I am not asking about the SelectedItem.

For example, I have a datagrid displaying a music library. I want to highlight a row whose music is being played. How can I do that in c# or using xaml?

I have tried using Style and a converter but I don't know what to bind.

The item source of the DataGrid is a public static List<Music> object.

    <controls:DataGrid
        x:Name="MusicLibraryDataGrid"
        Grid.Row="1"
        Margin="10"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        AlternatingRowBackground="WhiteSmoke"
        AlternatingRowForeground="Gray"
        AreRowDetailsFrozen="False"
        AreRowGroupHeadersFrozen="True"
        AutoGenerateColumns="False"
        CanUserReorderColumns="True"
        CanUserResizeColumns="True"
        CanUserSortColumns="True"
        ColumnHeaderHeight="32"
        DoubleTapped="MusicLibraryDataGrid_DoubleTapped"
        Foreground="Black"
        FrozenColumnCount="0"
        GridLinesVisibility="None"
        HeadersVisibility="Column"
        HorizontalScrollBarVisibility="Visible"
        IsDoubleTapEnabled="True"
        IsReadOnly="False"
        ItemsSource="{Binding AllSongs}"
        MinColumnWidth="100"
        SelectionMode="Extended"
        Sorting="MusicLibraryDataGrid_Sorting"
        VerticalScrollBarVisibility="Visible">
        <controls:DataGrid.Columns>
            // Some code
        </controls:DataGrid.Columns>
        <controls:DataGrid.RowStyle>
            <Style TargetType="controls:DataGridRow">
                <Setter Property="Foreground" Value="{Binding Music, Converter={StaticResource DataGridRowColorConverter}}" />
            </Style>
        </controls:DataGrid.RowStyle>
    </controls:DataGrid>
  • Look at this [thread](https://stackoverflow.com/questions/36277304/how-do-i-get-cell-row-and-column-indexes-of-uwp-grid-control) It would be helpful – Pavel Anikhouski Aug 15 '19 at 08:34
  • 1
    @PavelAnikhouski. I think that that is only for normal Grid controls, non for the DataGrid ones... – Luca Lindholm Aug 15 '19 at 09:06
  • It's hard to achieve what you want. It doesn't seem to provide APIs to get each item, so you can't bind the background. If you still want to implement the effect, I recommend you can use the ListView and then bind the background with a property in the viewmodel (e.g. isActive), when you want To highlight the background, set the property to true, and use convert to highlight the background color. – Faywang - MSFT Aug 15 '19 at 10:34
  • It's pretty frustrating to hear that especially WPF is provided such api...@Faywang-MSFT –  Aug 15 '19 at 14:11
  • Yes,for better customization, it's better to use ListView. – Faywang - MSFT Aug 19 '19 at 09:28

2 Answers2

1

Sorry for the late anwser but maybe it can help future people.

I was also with this problem. I wanted to highlight some rows whose the data was changed or was with some error.

I found out that using DataGrid.LoadingRow event you can access the row before it get instanciated and change some atributes:

dataGrid.LoadingRow += DataGrid_LoadingRow;
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    if ((e.Row.DataContext as string).Length == 0)
            e.Row.Background = new SolidColorBrush(Colors.Blue);
}

If the rows are already instanciated and you cant reload them, then this solution is not that useful. I hope it helps someone

Ink Archer
  • 153
  • 1
  • 8
0

Thanks to the @Faywang - MSFT's answer here, I have realized that, although there is no direct way to get a row in DataGrid, a workaround could be giving the ViewModel a property and use a converter to achieve what you want.

That question provides an example of ListView, but the idea is the same.