3

我有一个 datagridview 并试图根据一列和其他两列之间的比较结果动态更新行的背景颜色。我的 datagridview 绑定到一个数据表。datagridview 中的三个不同列是 min、max 和 present。min 和 max 列中的值是静态的,不会更改。每行的当前列中的值动态更新。

我使用了一个名为 MinMaxTester 的类,它实现了 IValueConverter 接口来比较单元格的内容以返回画笔颜色。

使用我实施的解决方案,我注意到背景颜色有时会更新。datagridview 是选项卡控件中选项卡项的一部分。当 datagridview 对用户不可见时,背景颜色通常会更新。当 datagridview 对用户可见时(即选项卡控件内的选项卡项已被选中),背景颜色不会更新。

我想知道我需要在我的解决方案中进行哪些更改以使行背景颜色始终更新?

XAML 文件代码

<Window.Resources>
    <local:MinMaxTester x:Key="MinMaxTester"/>
</Window.Resources>

<DataGrid.Columns>
<DataGridTextColumn Header="Present" Binding="{Binding Present}"/>
<DataGridTextColumn Header="Min" Binding="{Binding Min}"/>
<DataGridTextColumn Header="Max" Binding="{Binding Max}"/>
</DataGrid.Columns>

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
         <Setter Property="Background" Value="{Binding Converter={StaticResource MinMaxTester}}"/>
    </Style>
<DataGrid.RowStyle>

实施代码

[ValueConversion(typeof(DataRowView),typeof(Brush))]
public class MinMaxTester: IValueConverter
{
    public object Convert(object datagridrow, Type target, object parameter, System.Globalization.CultureInfo culture)
    {
        int min, max, present;
        DataRowView r = datagridrow as DataRowView;
        min = int.Parse(r["Min"].ToString());
        max = int.Parse(r["Max"].ToString());
        present = int.Parse(r["Present"].ToString());
        if (present >= min && present <= max) 
            return Brushes.Green;
        else
            return Brushes.Red;
    }
    public object ConvertBack(object datagridrow, Type target, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException("Not using the ConvertBack function in  MinMaxTester");

    }
}
4

3 回答 3

2

你试过这个吗?

<Window.Resources>
        <my:RowBackgroundColorConverter x:Key="rowBackgroundColorConverterResource"></my:RowBackgroundColorConverter>
    </Window.Resources>

   <DataGrid.RowStyle>
                            <Style TargetType="{x:Type DataGridRow}">
                                <Setter Property="Background" Value="{Binding fieldXXX, Converter={StaticResource converterXXX}}"></Setter>
                            </Style>
                        </DataGrid.RowStyle>

和转换器代码:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;

namespace XXX.Converters
{
    public class RowBackgroundColorConverter : IValueConverter
    {
        private readonly Color expiredColor = Colors.Red;

        private readonly Color normalColor = Colors.Gray;

        public object Convert(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {

            if (XXXXX)
                return new SolidColorBrush(expiredColor);

                return new SolidColorBrush(normalColor);
        }

        public object ConvertBack(
            object value, 
            Type targetType, 
            object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
于 2014-04-28T10:48:49.587 回答
1

我不确定您Present在做什么以及在哪里,但这可能会满足您的需要(无需过多更改代码)

<MultiBinding Converter="{StaticResource converter}" Mode="OneWay">
    <MultiBinding.Bindings>
        <Binding Path="Present" />
        <Binding Path="" />
    </MultiBinding.Bindings>
</MultiBinding>

...然后在你的转换器中(现在是IMultiValueConverter- 所有类似的只是一个字段)你有两个值。您使用“行”来计算。
...并且您使用直接绑定Present来触发更改。

您还需要确保“持有”现在的任何东西 - 已经提到了 INotifyPropertyChanged。

希望能帮助到你

于 2013-03-15T17:00:19.653 回答
0

您说您绑定到 a DataTable,但是 a 中的数据DataTable没有实现INotifyPropertyChanged,因此不会PropertyChange发出通知来告诉 UI 它需要更新。

我建议切换到将您DataGrid从 a绑定DataTable到a ObservableCollection<MyDataObject>,并确保MyDataObject实现INotifyPropertyChanged

于 2013-03-15T15:45:00.447 回答