WPF - How to get a cell from a DataGridRow?

前端 未结 2 1430
南笙
南笙 2021-02-08 07:54

I have a data-bound DataGrid with alternating row background colors. I would like to color a cell differently based on the data it contains. I have tried the solution suggested

相关标签:
2条回答
  • 2021-02-08 08:06

    If you know your row and index of the cell you'd like to access, then here's how you can do it in code:

    //here's usage
    var cell = myDataGrid.GetCell(row, columnIndex);
    if(cell != null)
        cell.Background = Brushes.Green;
    

    DataGrid Extension:

    public static class DataGridExtensions
    {
        public static DataGridCell GetCell(this DataGrid grid,  DataGridRow row, int columnIndex = 0)
        {
            if (row == null) return null;
    
            var presenter = row.FindVisualChild<DataGridCellsPresenter>();
            if (presenter == null) return null;
    
            var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
            if (cell != null) return cell;
    
            // now try to bring into view and retreive the cell
            grid.ScrollIntoView(row, grid.Columns[columnIndex]);
            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
    
            return cell;
        }
    
    0 讨论(0)
  • 2021-02-08 08:22

    First thing first, don't do this in code-behind. You're fighting the framework with this way of doing things. WPF is designed differently; you have to think in terms of how the framework wants you to do things. In the case of WPF, it's XAML markup + converter classes.

    You need two things to achieve what you want:

    • Proper XAML markup to setup the style of the DataGrid
    • An IValueConverter implementation to translate the value of the text into proper highlight color.

    Here goes:

    XAML In Your Datagrid

    The first thing you want to do is define the XAML necessary to style your DataGrid cells. It looks like this:

    <toolkit:DataGrid.CellStyle>
          <Style TargetType="{x:Type toolkit:DataGridCell}">
            <Style.Setters>
              <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource dataGridCellConverter}}" />
            </Style.Setters>
          </Style>
        </toolkit:DataGrid.CellStyle>
    

    What this is doing is setting up a binding to the RelativeSource (the DataGridCell) and telling it to use the Content.Text of the cell as the value to pass to the Converter (dataGridCellConverter).

    IValueConverter

    The next thing you need is an IValueConverter implementation to actually determine the colors based on the text of the cell:

    using System;
    using System.Globalization;
    using System.Windows.Data;
    using System.Windows.Media;
    namespace UserControls.Utility.Converters
    {
      public class DataGridCellConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          if (value == null) return Colors.White.ToString();
    
          if (value.ToString().ToUpper().Contains("CMS")) return "LIME";
    
          return "ORANGE";
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    }
    

    Here, I'm just looking for the text "CMS" and coloring the background cell; if "CMS" doesn't exist, then it returns the orange color instead.

    Specify Resources

    Now, you need to add markup in your window/usercontrol to specify the converter as an appropriate resource:

    <UserControl.Resources>
        <Converters:DataGridCellConverter x:Key="dataGridCellConverter"/>
    </UserControl.Resources>
    

    And that should do it! Good luck.

    0 讨论(0)
提交回复
热议问题