Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
165
Simple way to set the background of a cell based on it's field name
posted

I want all of the cells in a column to have the same background color. Seems like it should be pretty simple but I can't find a simple solution.

Parents
  • 4850
    Suggested Answer
    Offline posted

    Yeah, sometime in WPF the simple things can be a little more involved. To do what you want to do the best thing is to change the style for the CellValuePresenter for that Field. Field.Settings exposes a property for this called CellValuePresenterStyle. You can set this in xaml or by listening to the FieldLayoutInitialized event. e.g.:

     private void XDG_FieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e)

    {

     

    Style deptStyle = new Style(typeof(CellValuePresenter));

     

     

     

    Setter setter = new Setter();

     

     

     

    setter.Property = CellValuePresenter.BackgroundProperty;

     

     

     

    setter.Value = Brushes.Yellow;

     

     

     

    deptStyle.Setters.Add(setter);

     

     

     

    deptStyle.Seal();

     

     

     

    foreach (Field field in e.FieldLayout.Fields)

     

     

     

    if (field.Name == "department")

    {

     

     

     

    field.Settings.CellValuePresenterStyle = deptStyle;

     

     

     

    break;

    }

    }

Reply Children
No Data