2017-09-26 115 views
0

我有改变的DataGrid行的颜色问题,我使用下面的函数在DataGrid更改行的颜色WPF

int i = 0; 
private void gvDados_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
    DataGridRow rowContext = e.Row; 
    if (rowContext != null) 
    { 
     string Situacao = dt.Rows[i]["Situacao"].ToString(); 
     if (Situacao.Equals("V")) 
     { 
     SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(100, 255, 104,0)); 
     rowContext.Background = brush; 
     } 
     else 
     { 
     SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(100, 255, 232,0)); 
     rowContext.Background = brush; 
     } 

     i++; 
    } 
} 

到目前为止好,我可以根据调整颜色是什么我想,问题在于当我使用水平滚动条沿着寄存器或攀爬时,随机出现所有颜色的错误配置。我该如何解决这个问题,以便在滚动时不会改变?

回答

0

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中的每个项目)设置为显示每行所需的颜色。

+0

我需要以编程方式执行此操作如何读取数据网格中的每一行并更改其颜色?使用一段时间,并根据我的情况改变颜色 –

+1

WPF方法_is_以编程方式执行。您只需让XAML执行着色,而不是在代码后面进行着色。但是如果您必须使用代码,请参阅我发布的更新。在DataGrid行上使用'foreach'循环,并将行传递给为它们着色的函数。使用foreach(DataGridRow排在gvData) {myfunction的()} – Sach

+0

错误。让我更新一个关于如何获得每一行的例子。 –