这是我关于stackoverflow的第一个问题,即使我已经使用它2年了。 (非常有帮助)。很抱歉,如果没有正确询问。
我将项目从WinForms转移到WPF,我遇到了一些麻烦。 我有一个自动填充SQL请求的数据网格,当单元格格式化事件时,DataGridViewCellFormatting'被触发了。 我使用此事件使线条颜色不同。 (更加用户友好)
WinForm上的代码:
private void ChangerCouleur(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
row.DefaultCellStyle.SelectionBackColor = Color.Orange;
row.DefaultCellStyle.SelectionForeColor = Color.Black;
if (e.RowIndex % 2 == 0)
{
row.DefaultCellStyle.BackColor = Color.Khaki;
row.DefaultCellStyle.ForeColor = Color.Black;
}
else
{
row.DefaultCellStyle.BackColor = Color.Goldenrod;
row.DefaultCellStyle.ForeColor = Color.Black;
}
}
我无法在WPF中找到相同的事件。
提前致谢
答案 0 :(得分:2)
DataGridCell
以及每个WPF可视项目都包含Initialized
个事件。为了您的目的,这可能是您正在寻找的。如果您需要与项目进行互动,还会发生Loaded
事件
第一次布置和渲染。
您可能会发现使用DataGrid.AlternatingRowBackground
纯粹使用XAML可以达到理想的效果:
<DataGrid RowBackground="Khaki"
AlternatingRowBackground="Goldenrod"
Foreground="Black">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Orange"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>
答案 1 :(得分:1)
查看您的代码示例,我想您想要更改交替行的颜色吗?
如果是这样,你可以在XAML stlye中这样做:
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="#FFF" />
<Setter Property="AlternationCount" Value="2" />
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="Khaki"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Goldenrod"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>