![](/img/trans.png)
[英]Change background of WPF DataGrid cell depending on value when populated with a datatable as IListSource
[英]Change Background of Datagrid row depending on datatable Value
我正在努力解決一個看似簡單的問題:我有一個綁定到數據表的數據網格。 該數據表包含一個名為“ COLORSTATUS”(枚舉值)的列-我想根據ColorStatus繪制數據表的每個ROW。 我試圖建立一個值轉換器-但我無法將其傳遞給整行和/或數據表。 我已經想過將Caligris Messages與死死的DataGridRow事件掛鈎-但是如何在XAML中執行此操作-我只能訪問DataGrid.RowStyle Elements。
<DataGrid x:Name="excelDataTable_ExcelData" cal:Message.Attach="[Event AutoGeneratedColumns] = [Action HideTheColorColumn($source)]">
?? What to do here
</DataGrid>
我決定采用以下解決方案:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Row, Converter={StaticResource ExcelRowColorConverter}}"></Setter>
</Style>
</DataGrid.RowStyle>
我有點驚訝,您可以通過“行”。 作為轉換器的提示:傳遞的對象是實際的DataRow。
轉換器應該可以正常工作,但是您必須確保將其應用在正確的位置。
只需將其傳遞給DataRow
,從中獲取“ COLORSTATUS”列值,然后返回適當的顏色筆刷即可。
例如,
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow }">
<!-- DataContext will be your DataRow -->
<Setter Property="Background"
Value="{Binding Converter={StaticResource MyColorConverter}}" />
</Style>
</DataGrid.Resources>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.