更改DataGrid的颜色

时间:2017-06-13 13:08:58

标签: c# .net wpf datagrid

我想用当前代码更改整行的颜色,但INNER JOIN referenceinfo ON message.mid = referenceinfo.mid row is null不存在。

我想突出显示3 rd 行。

datagrid.Rows

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
          <Style.Triggers>
                <DataTrigger Binding="{Binding Executed}" Value="False">
                      <Setter Property="Background" Value="LightCoral" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Executed}" Value="True">
                      <Setter Property="Background" Value="LightGreen" />
                </DataTrigger>
          </Style.Triggers>     
    </Style>
</DataGrid.RowStyle>

在这种情况下,我使用caliburn micro来绑定背景颜色,具体取决于我的行中的bool(使用bool?保持白色,直到更改bool)。

答案 1 :(得分:0)

这不是更改DataGridRow背景的最佳方式 - 您应该使用@David Danielewicz建议的Style - 但是对于您当前的工作方法,您应该投射对象从方法返回到系统 .Windows.Controls.DataGridRow。

您还应该使用ContainerFromIndex方法来获取第四个元素的可视容器的引用。第三个元素的索引为2.

试试这个:

var row = datagrid.ItemContainerGenerator.ContainerFromIndex(2) as System.Windows.Controls.DataGridRow;
row.Background = Brushes.Blue;

另请注意,为此,您需要等到实际创建容器:

datagrid.Loaded += (ss, ee) => 
{
    var row = datagrid.ItemContainerGenerator.ContainerFromIndex(2) as System.Windows.Controls.DataGridRow;
    row.Background = Brushes.Blue;
};

答案 2 :(得分:0)

从代码中访问View是一种不好的做法。更好地利用MVVM的力量:

<Window>
    <Window.Resources>
        <ResourceDictionary>
            <Style x:Key="DataGridRowStyle" TargetType="DataGridRow">
                <Setter Property="Background" Value="{Binding RowBackground}"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <DataGrid ItemsSource="{Binding Records}" RowStyle="{StaticResource DataGridRowStyle}" AutoGenerateColumns="False" CanUserAddRows="False">        
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Value}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>    
</Window>

public MainWindow()
{
    InitializeComponent();
    DataContext = new MainWindowViewModel(); 
}

public class MainWindowViewModel
{
    public MainWindowViewModel()
    {
        Records.Add(new RecordViewModel()
        {
            Value = "Red",
            RowBackground = new SolidColorBrush(Colors.LightCoral)
        });

        Records.Add(new RecordViewModel()
        {
            Value = "Green",
            RowBackground = new SolidColorBrush(Colors.LightGreen)
        });

        Records.Add(new RecordViewModel()
        {
            Value = "Blue",
            RowBackground = new SolidColorBrush(Colors.LightBlue)
        });

        Records[2].Value = "Not blue anymore";
        Records[2].RowBackground = new SolidColorBrush(Colors.LightPink);
    }

    public ObservableCollection<RecordViewModel> Records { get; } = new ObservableCollection<RecordViewModel>();
}

public class RecordViewModel : INotifyPropertyChanged
{
    private string _value;
    private Brush _rowBG;
    public event PropertyChangedEventHandler PropertyChanged;

    public string Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            OnPropertyChanged(nameof(Value));
        }
    }

    public Brush RowBackground
    {
        get
        {
            return _rowBG;
        }
        set
        {
            _rowBG = value;
            OnPropertyChanged(nameof(RowBackground));
        }
    }

    private void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}