Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

DataGrid Run-Time Cell Backcolor Update

$
0
0

All, I have seen and asked the following respectively

1. http://stackoverflow.com/questions/15875040/how-to-change-a-datagrid-row-colour-when-a-certain-value-is-encountered

2. http://stackoverflow.com/questions/15644105/change-the-background-color-of-entire-column-of-wpf-datagrid-at-runtime

I never received a sufficient answer to the latter and again I am really struggling to start doing what I want.

I have a `DataGrid` which at run-time I load with data via

    dataGrid.ItemsSource = BuildDataGridColumns(cultureDict, additionalDt).Tables[0].AsDataView();

Where

    private DataSet BuildDataGridColumns(Dictionary<string, string> cultureDict,
                                         DataTable additionalDt = null)
    {
        // Get the data.
    }

All I want is to do is update the background colour of cells where the contents matches a specified string. So in a text box the user might want to find 'itemX', and I want to change the background colour of the relevant cells. Due to the fact I never got a sufficient answer for the above question, I did this 'the wrong way' which I will show at the end of this question.

**I would like some one to show me how this can be achieved '_the right way' with `Trigger`s or `Commands`?**

I am really struggling with WPF as I am relatively new to commercial programming and have been working solely with WinForms since I started.

---

_How I did this_ the WinForms way:

    bool checksOk = true;
    const int baseColumnCount = 3;
    for (int i = baseColumnCount; i < dataGrid.Columns.Count; i++)
    {
        foreach (DataGridRow row in Utilities.GetDataGridRows(dataGrid))
        {
            if (row != null)
            {
                DataGridCell cell = dataGrid.GetCell(row, i);

                // Start work with cell.
                Color color;
                TextBlock tb = cell.Content as TextBlock;
                string cellValue = tb.Text;
                if (!CheckForBalancedParentheses(cellValue))
                {
                    color = (Color)ColorConverter.ConvertFromString("#FF0000");
                    checksOk = false;
                }
                else
                    color = (Color)ColorConverter.ConvertFromString("#FFFFFF");
                //row.Background = new SolidColorBrush(color);
                cell.Background = new SolidColorBrush(color);
            }
        }
    }
   
_To do this I needed the following utility methods:_

    public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }
   
    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
                child = GetVisualChild<T>(v);
            if (child != null)
                break;
        }
        return child;
    }
   
_Now that is nasty! [Chris Tucker voice] Please tell me how I should be doing this._


"Everything should be made as simple as possible, but not simpler" - Einstein


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>