LoadingRow
事件DataGrid
在被「實例化」時觸發。當你滾動行進入和退出範圍,並且這個事件被重複觸發,每一行都加載到視圖中。
假設你DataGrid
在某些事件時加載,如單擊按鈕或一些這樣的行動,你可能需要做行的顏色當你真正將數據加載到DataGrid
,最好使用該寫的功能,如果內容發生變化,並且您想根據更改的內容顯示顏色,請稍後再次調用該函數。
事情是這樣的:
// This could be a button event, or some other event after which you load data into the DataGrid
void ButtonLoadEvent()
{
foreach(Datagrid Row)
{
FunctionThatChangesRowColor(Row);
}
}
編輯:
如何獲得數據網格行和應用着色的實際代碼。這是一個着色邏輯的修改版本,這裏每一行都根據其行索引是奇數還是偶數來着色。你可以用你的代碼替換它。
將整個DataGrid
傳遞給此函數。
private void ColorRow(DataGrid dg)
{
for (int i = 0; i < dg.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
if (row != null)
{
int index = row.GetIndex();
if (index % 2 == 0)
{
SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(100, 255, 104, 0));
row.Background = brush;
}
else
{
SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(100, 255, 232, 0));
row.Background = brush;
}
}
}
}
,但同樣是沒有看到你正在使用WPF,而不是一個WinForms的完美的解決方案。我有一個建議是採用WPF DataBinding方法,讓XAML爲您做顏色編碼。
This是我經常用於此目的的代碼示例。
WPF方法編碼:
<Window x:Class="ColorLibrary.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ColorLibrary"
mc:Ignorable="d"
Loaded="Window_Loaded"
Title="MainWindow" Height="500" Width="400">
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Setters>
<Setter Property="Background" Value="{Binding Path=Code}"></Setter>
</Style.Setters>
</Style>
</Window.Resources>
<Grid>
<!-- Stuff -->
<DataGrid Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"
Name="dgvColors"
AutoGenerateColumns="False"
Margin="4">
<DataGrid.Columns>
<DataGridTemplateColumn Header="#" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Num}" VerticalAlignment="Center" Padding="3"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="HTML Code" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Code}" VerticalAlignment="Center" Padding="3"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Color" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Color}" VerticalAlignment="Center" Padding="3"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
在,
<Setter Property="Background" Value="{Binding Path=Code}"></Setter>
Code
是其中包含要用於着色的細胞的顏色名稱的類的屬性。
然後我們有這個類對象的ObservableCollection
。您需要將此屬性(ObservableCollection
中的每個項目)設置爲顯示每行所需的顏色。
我需要以編程方式執行此操作如何讀取數據網格中的每一行並更改其顏色?使用一段時間,並根據我的情況改變顏色 –
WPF方法_is_以編程方式執行。您只需讓XAML執行着色,而不是在代碼後面進行着色。但是如果您必須使用代碼,請參閱我發佈的更新。在DataGrid行上使用'foreach'循環,並將行傳遞給爲它們着色的函數。使用foreach(DataGridRow排在gvData) {myfunction的()} – Sach
錯誤。讓我更新一個關於如何獲得每一行的例子。 –