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
1052
Cell formatting in XamGrid
posted

Hi,

I  have a masterdetail  grid for which I  need to apply a gradient of background colours for the detail as well as particular columns in the master grid.

I tried using ThreeColorConditionalFormatting for it, but the colour shown as the cell background wasn’t really a representation of the colour between the maximum and minimum values.

As show in the attached example the weight column in the master has a value of 16. And I need the colour of this cell to be represented by where it lies between the maximum and the minimum weight.

But it always shows me a green colour.

 

I also tried using the Cellcontrolattached event and having my own gradient generator to set the back colour but in the case of a large dataset the scrolling doesn’t reflect the colour and a grey background appears.

 

Is there any other way of achieving this?

SilverlightApplication10.zip
  • 40030
    Verified Answer
    Offline posted

    Hi, 

    Yes, you can achieve this with a CustomConditionalFormatRule. 

    Basically, you just need a way of setting the Median, Maxium and Minimum values for the Rule, as you can't do them through a binding. 

    Probably the simplest way to do this, is through deriving your own ThreColorScaleConditionalFormatRule

    Just follow the steps below: 

    1. Derive from ThreeColorScaleConditionalFormatRule:

    public class CustomThreeColorScaleConditionalFormatRule : ThreeColorScaleConditionalFormatRule

        {

            protected override IConditionalFormattingRuleProxy CreateProxy()

            {

                return new CustomThreeColorScaleConditionalFormatRuleProxy() { Median = this.MedianValue, Maximum = this.MaximumValue, Minimum = this.MinimumValue };

            }

        }

     

    2. Derive from ThreeColorScaleConditionalFormatRuleProxy (this is the workhorse that performs all of the logic, here, we'll need to set the Median, Maximum, and Minimum values before we execute:

    public class CustomThreeColorScaleConditionalFormatRuleProxy : ThreeColorScaleConditionalFormatRuleProxy

        {

            public double? Median

            {

                get { return this.MedianValue; }

                set { this.MedianValue = value; }

            }

     

            public double? Maximum

            {

                get { return this.MaximumValue; }

                set { this.MaximumValue = value; }

            }

     

            public double? Minimum

            {

                get { return this.MinimumValue; }

                set { this.MinimumValue = value; }

            }

     

            protected override Style EvaluateCondition(object sourceDataObject, object sourceDataValue)

            {

                if (sourceDataValue == null)

                {

                    return null;

                }

     

                Summary summary = sourceDataObject as Summary;

                if (summary != null)

                {

                    this.MaximumValue = summary.MaxWeight;

                    this.MinimumValue = summary.Minweight;

     

                    IQueryable q = summary.Students.AsQueryable();

     

                    SummaryContext sc = SummaryContext.CreateGenericSummary(typeof(Student), "Weight", LinqSummaryOperator.Average);

                    this.MedianValue = Convert.ToDouble(sc.Execute(q), CultureInfo.InvariantCulture);

     

                    this.GenerateLineEquations();

                }

     

                return base.EvaluateCondition(sourceDataObject, sourceDataValue);

     

            }

        }

    3. Use this Rule instead of the in the box ThreColorScaleRule:

     <infragistics:TextColumn Key="OurWeight" Width="*" IsSortable="True" IsSorted ="Ascending">

                        <infragistics:TextColumn.ConditionalFormatCollection>

                            <local:CustomThreeColorScaleConditionalFormatRule  MaximumColor="Salmon" MedianColor="#FFD6C347" MinimumColor="#FF76D264" MaximumValue="{Binding RowData.MaxWeight}" MinimumValue="{Binding RowData.Minweight}"/>

                        </infragistics:TextColumn.ConditionalFormatCollection>

     

    Hope this helps, 

    -SteveZ