[英]UWP Datagrid Get Row
如何在 UWP 中獲得一行? 我在 WPF 中找到了解決方案,但 UWP 似乎沒有這些選項。
我不是在問SelectedItem
。
例如,我有一個顯示音樂庫的數據網格。 我想突出顯示正在播放其音樂的行。 我如何在 c# 或使用 xaml 中做到這一點?
我曾嘗試使用 Style 和轉換器,但我不知道要綁定什么。
DataGrid 的項目源是一個public static List<Music>
對象。
<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>
對不起,遲到的回答者,但也許它可以幫助未來的人。
我也遇到了這個問題。 我想突出顯示一些數據已更改或出現錯誤的行。
我發現使用DataGrid.LoadingRow事件您可以在它被實例化之前訪問該行並更改一些屬性:
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);
}
如果行已經實例化並且您無法重新加載它們,那么這個解決方案就沒有那么有用了。 我希望它可以幫助某人
感謝@Faywang - MSFT 在這里的回答,我意識到,雖然沒有直接的方法在DataGrid
獲得一行,但一種解決方法可能是為 ViewModel 提供一個屬性並使用轉換器來實現您想要的。
那個問題提供了一個ListView的例子,但是思路是一樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.