Search code examples
c#uwpdatagrid

UWP Datagrid Get Row


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>

Solution

  • 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.