【问题标题】:Problems changing datagrid color using WPF C#使用 WPF C# 更改数据网格颜色的问题
【发布时间】:2018-03-12 10:01:39
【问题描述】:

我正在使用数据网格并根据行的条件更改行的颜色,并且我正在以编程方式执行此操作。 根据例子 由于我的数据网格绑定到数据表,因此我直接从数据表加载信息

private void UpdateCor () {

gvDados.UpdateLayout ();
for (int i = 0; i <dt.Rows.Count; i ++)
{
      var rowContext = (DataGridRow) 
      gvDados.ItemContainerGenerator.ContainerFromIndex (i);

      if (rowContext! = null)
      {
            if (dt.Rows [i] ["situation"]. ToString (). Equals (1))
                     rowContext.Background = Brushes.Green;
            else
                     rowContext.Background = Brushes.Red;
      }
   }
}

有了这个我可以更新我的网格的颜色,即使它不是最好的方法。我的问题是,每当我使用滚动条向下或向上移动时,颜色都会过时。我该如何防止这种情况发生?即使我滚动条,颜色也保持不变?

【问题讨论】:

  • 此链接 :datatrigger on enum to change image 可能会对您有所帮助。可能只需要设置背景颜色
  • 这是因为虚拟化。您正在尝试做的是一个非常糟糕的主意,可能无法使工作正确。您应该在 XAML 中使用样式和触发器执行此操作。 Stack Overflow 上有很多例子。但是使用谷歌搜索;这里的搜索功能不是很好。

标签: c# wpf datagrid wpfdatagrid


【解决方案1】:

XAML 通常过于简单,无法表达更复杂的条件。我更喜欢将哪些值应该使用哪些颜色的逻辑放入转换器中。这为 C# 中的转换器带来了更简单的 XAML 和更大的灵活性。

<datagrid.rowstyle>
  <style targettype="DataGridRow">
    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self},
      Path=Item.situation, Converter={StaticResource ValueToBackgroundConverter}}"/>
  </style>
</datagrid.rowstyle>

在 C# 中:

class ValueToBackgroundConverter: IValueConverter {
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    if (value is int) {
      int quantity = (int)value;
      if (quantity>=100) return Brushes.White;
      if (quantity>=10) return Brushes.WhiteSmoke;
      if (quantity>=0) return Brushes.LightGray;
      return Brushes.White; //quantity should not be below 0
    }
    //value is not an integer. Do not throw an exception
    // in the converter, but return something that is obviously wrong
    return Brushes.Yellow;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
    throw new NotImplementedException();
   }
}

格式化 WPF Datagrid 的各个部分是出了名的困难,Microsoft 没有提供如何做到这一点的必要信息。阅读我的文章Codeproject: Guide to WPF DataGrid formatting using binding 以更好地了解如何轻松完成。

【讨论】:

    【解决方案2】:

    这是与this question 类似的问题。 可以使用数据触发器完成:

     <DataGrid ItemsSource="{Binding YourItemsSource}">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow"> 
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding State}" Value="State1">
                            <Setter Property="Background" Value="Red"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding State}" Value="State2">
                            <Setter Property="Background" Value="Green"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 2012-04-20
      • 2012-10-26
      • 2011-02-20
      • 2011-05-28
      • 2018-05-31
      • 2022-01-14
      • 2018-02-02
      • 2013-10-09
      相关资源
      最近更新 更多