我需要爲我在運行時添加到DataTable的行分配顏色。如何才能做到這一點?以編程方式爲DataGrid中的行分配顏色
19
A
回答
36
您可以處理DataGrid的LoadingRow事件以檢測何時添加行。在事件處理程序中,您可以獲取DataRow的引用,該引用已添加到充當您的ItemsSource的DataTable中。然後,您可以更新DataGridRow的顏色,只要你喜歡。
void dataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
{
// Get the DataRow corresponding to the DataGridRow that is loading.
DataRowView item = e.Row.Item as DataRowView;
if (item != null)
{
DataRow row = item.Row;
// Access cell values values if needed...
// var colValue = row["ColumnName1]";
// var colValue2 = row["ColumName2]";
// Set the background color of the DataGrid row based on whatever data you like from
// the row.
e.Row.Background = new SolidColorBrush(Colors.BlanchedAlmond);
}
}
要註冊的事件在XAML:
<toolkit:DataGrid x:Name="dataGrid"
...
LoadingRow="dataGrid_LoadingRow">
或者在C#:
this.dataGrid.LoadingRow += new EventHandler<Microsoft.Windows.Controls.DataGridRowEventArgs>(dataGrid_LoadingRow);
1
重要:一定要始終指定默認值不在行被一種條件或任何其他風格着色。
查看我的回答C# Silverlight Datagrid - Row Color Change。
PS。我在Silverlight並沒有證實在WPF
這種行爲
10
U可以試試這個
在XAML
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Setters>
<Setter Property="Background" Value="{Binding Path=StatusColor}"></Setter>
</Style.Setters>
</Style>
</Window.Resources>
在DataGrid
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Name="dtgTestColor" ItemsSource="{Binding}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Valor" Binding="{Binding Path=Valor}"/>
</DataGrid.Columns>
</DataGrid>
在代碼中,我有一類
public class ColorRenglon
{
public string Valor { get; set; }
public string StatusColor { get; set; }
}
當設置在DataContext
dtgTestColor.DataContext = ColorRenglon;
dtgTestColor.Items.Refresh();
如果妳不設置行的默認值是灰色
的顏色ü可以試試這個樣本 這個樣本
List<ColorRenglon> test = new List<ColorRenglon>();
ColorRenglon cambiandoColor = new ColorRenglon();
cambiandoColor.Valor = "Aqui va un color";
cambiandoColor.StatusColor = "Red";
test.Add(cambiandoColor);
cambiandoColor = new ColorRenglon();
cambiandoColor.Valor = "Aqui va otro color";
cambiandoColor.StatusColor = "PaleGreen";
test.Add(cambiandoColor);
相關問題
- 1. 以編程方式從顏色中獲取選擇的顏色
- 2. 使用wpf以編程方式更改datagrid選擇的行顏色?
- 3. 以R.編程方式檢索顏色
- 4. Matplotlib:爲行分配顏色
- 5. 以編程方式藍色選擇Datagrid行
- 6. 顏色以編程方式分隔的某些區域
- 7. 以編程方式更改ListView行的背景顏色(wpf)
- 8. 以編程方式在Android中刪除色調顏色
- 9. 如何以編程方式獲取ImageView的色調顏色?
- 10. asp:表以編程方式更改行顏色
- 11. 如何以編程方式設置tablelayout行背景顏色
- 12. 如何以編程方式更改Spotlight中一組顏色方案分組的顏色(IronPython)?
- 13. 以編程方式更改全局色調顏色在運行時iOS8中,Swift
- 14. 如何以編程方式反轉Linux中的屏幕顏色
- 15. 如何以編程方式更改Highcharts中的顏色選項?
- 16. 以編程方式設置GWT中標籤的文字顏色
- 17. 如何更改android中的SeekBar顏色? (以編程方式)
- 18. 如何以編程方式更改圖像中的顏色?
- 19. 以編程方式如何重置PDF中的顏色
- 20. iPad編程,任何方式來以編程方式反轉顏色?
- 21. 如何以編程方式將活動的背景顏色設置爲白色?
- 22. 如何以編程方式爲數據網格行設置背景顏色
- 23. 以編程方式在C中更改顏色#
- 24. 以編程方式添加DataGrid Row?
- 25. 格式DataGrid列顏色
- 26. 如何以編程方式將狀態欄顏色更改爲黑色
- 27. 如何編程修改DataGrid行的顏色在WPF?
- 28. 以編程方式更改狀態欄顏色等於導航欄顏色
- 29. 如何以編程方式設置UILabel的背景顏色?
- 30. 以編程方式設置listview的背景顏色
保證分配默認值行的顏色不是由條件 – 2010-01-17 02:53:09
謝謝。這對我來說是一個驚人的簡單方法。 – Nasenbaer 2011-08-11 14:40:51
不起作用。項目始終爲空 – Yusha 2017-12-29 21:18:18