繁体   English   中英

在Wpf中更改DataGrid中行的颜色

[英]Changing the color of rows in a DataGrid in Wpf

我在更改DataGrid行的颜色时遇到问题,我使用以下功能

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++;
   }
}

到目前为止,我可以根据自己的需要调整颜色,问题是当我使用水平滚动条向下滚动寄存器或爬升时,所有颜色都被错误地配置为随机出现。 我该如何解决此问题,以便滚动时事物不会改变?

LoadingRow的事件DataGrid每当火Row是“实例”。 当您滚动行时,行将进出作用域,并且超出范围,并且此事件将被“加载”到视图的每一行重复触发。

假设你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行和应用着色的实际代码。 这是着色逻辑的修改版本,此处每行的颜色取决于其行索引是奇数还是偶数。 您可以将其替换为您的代码。

将整个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为您进行颜色编码。

是我经常为此目的使用的代码示例。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM