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
235
xamGrid Conditional Formatting in code
posted

Greetings!

I have a xamGrid for an Engineering application where the columns are determined at runtime.

The column definitions (including group columns and fixed columns) are retrieved from an Oracle data source using Entity framework, and served to the silverlight client through RIA services.  Due to the flexible nature of the data, all of the columns are added as type TextColumn. I can add the columns and groups without any trouble.  In my code to load the columns, I have specified a "ContainingConditionalFormatRule" to apply a style to cells with a certain value as follows:

 

ContainingConditionalFormatRule notApplicableRule = new ContainingConditionalFormatRule();

notApplicableRule.CellValueVisibility = System.Windows.Visibility.Collapsed;

notApplicableRule.StyleScope = StyleScope.Cell;

notApplicableRule.ShouldRefreshOnDataChange = true;

notApplicableRule.StyleToApply = App.Current.Resources["NotApplicableStyle"] as Style;

notApplicableRule.Value = "#!#";

 

The style resource is defined as:

 

 <!-- Not applicable cell style -->

<Style x:Key="NotApplicableStyle" TargetType="ig:ConditionalFormattingCellControl">

<Setter Property="Foreground" Value="#FF4C4545" />

       <Setter Property="Background" Value="#FF4C4545"/>

       <Setter Property="AltBackground" Value="#FF4C4545"/>

 </Style>

 

Each time a column definition is added to the grid, it includes a the following to set add the conditional formatting rule:

 

col.ConditionalFormatCollection.Add(notApplicableRule);

 

 Each of the data columns has a key that matches a property definition similar to:

 

[DataMember()]

[Required()]

[StringLength(2000)]

 public string Entered1{

       get{ return this._entered1;}

       set{

              if ((this._entered1 != value)){

                     this.OnEntered1Changing(value);

                     this.RaiseDataMemberChanging("Entered1");

                     this.ValidateProperty("Entered1", value);

                     this._entered1 = value;

                     this.RaiseDataMemberChanged("Entered1");

                     this.OnEntered1Changed();

}

}

}

 

Later on, the user will execute a server-side filter to populate the columns.  The data is returned as a List<UsrBrowserRowdataSession>, where UsrBrowserRowdataSession is an Entity class returned by my DomainService.  I use this list to be the ItemSource of my grid.

When I view the resulting grid, the style does not get applied to each of the columns that contain the value "#!#".  However, if I implement the CellControlAttached event handler instead, I am able to inspect the e.Cell.Value.ToString() == "#!#" to apply the same style to the cell. I would rather have the conditional formatting engine do the work, since it does more for me.  Does anyone have any thoughts why my Conditional Formatting rule doesn't work, but formatting using the CellControlAttached does?

Parents
  • 40030
    Offline posted

    Hi, 

    You need to specify what type of rule, you're currently using the base class which won't do anything:

     grid.ConditionalFormattingSettings.AllowConditionalFormatting = true;

     

                TextColumn col = new TextColumn() { Key = "ChapterNumber" };

     

                EqualToConditionalFormatRule notApplicableRule = new EqualToConditionalFormatRule();

                notApplicableRule.CellValueVisibility = System.Windows.Visibility.Collapsed;

                notApplicableRule.StyleScope = StyleScope.Cell;

                notApplicableRule.ShouldRefreshOnDataChange = true;

                notApplicableRule.StyleToApply = this.Resources["NotApplicableStyle"] as Style;

                notApplicableRule.Value = 1998;

                col.ConditionalFormatCollection.Add(notApplicableRule);

                grid.Columns.Add(col);

    Hope this helps, 

    -SteveZ

     

     

Reply Children