我需要爲我在運行時添加到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. 以編程方式更改colorControlActivated顏色
- 5. 以編程方式更新顏色
- 6. 以編程方式更改GridView控件的行顏色格式
- 7. Matplotlib:爲行分配顏色
- 8. 以編程方式藍色選擇Datagrid行
- 9. 顏色以編程方式分隔的某些區域
- 10. 以編程方式更改ListView行的背景顏色(wpf)
- 11. 以編程方式在Android中刪除色調顏色
- 12. 如何以編程方式獲取ImageView的色調顏色?
- 13. 如何以編程方式更改imageView的色調顏色?
- 14. 如何以編程方式設置tablelayout行背景顏色
- 15. asp:表以編程方式更改行顏色
- 16. 以編程方式將顏色設置爲繪製視圖
- 17. 如何以編程方式更改Spotlight中一組顏色方案分組的顏色(IronPython)?
- 18. 更改值的Datagrid樣式行顏色
- 19. 以編程方式更改全局色調顏色在運行時iOS8中,Swift
- 20. 如何更改android中的SeekBar顏色? (以編程方式)
- 21. 以編程方式如何重置PDF中的顏色
- 22. 如何以編程方式確定TextView中文本的顏色?
- 23. 如何以編程方式更改colors.xml中的顏色值?
- 24. 如何以編程方式更改圖像中的顏色?
- 25. 如何以編程方式更改Highcharts中的顏色選項?
- 26. 以編程方式設置GWT中標籤的文字顏色
- 27. 如何以編程方式反轉Linux中的屏幕顏色
- 28. 以編程方式更改層列表中形狀的顏色
- 29. 以編程方式在顏色選擇器中向下移動顏色陰影?
- 30. 以編程方式取消選擇DataGrid中的一行
保證分配默認值行的顏色不是由條件 – 2010-01-17 02:53:09
謝謝。這對我來說是一個驚人的簡單方法。 – Nasenbaer 2011-08-11 14:40:51
不起作用。項目始終爲空 – Yusha 2017-12-29 21:18:18