This is a migrated thread and some comments may be shown as answers.

Format Conditions

4 Answers 101 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marcello
Top achievements
Rank 1
Iron
Marcello asked on 06 Dec 2017, 04:19 PM

Hi,

I created a little UI that allow user to personalize the GridView properties.

I a section of this UI, a user can add a "Format Condition" defining an Expression (using RedExpressions) and some Style preferences, in example a row background.

When a FormatCondition class is created, in the constructor, I add a StyleRule to GridView.RowStyleSelector, I bind the StyleRuleCondition to FormatCondition.Condition and I'm waiting for BackGround changes:

            Grid = g;
            Css = (ConditionalStyleSelector)(g.RowStyleSelector ?? (g.RowStyleSelector = new ConditionalStyleSelector()));
            StyleRule = new StyleRule();
            StyleRule.Bind(StyleRule.ConditionProperty, Condition, converter: new Converters.FunctionConverter<string, System.Linq.Expressions.Expression>(Convert));
            StyleRule.Style = new System.Windows.Style(typeof(GridViewRow)
, (Style)Application.Current.FindResource("GridViewRowStyle"));
            Css.Rules.Add(StyleRule);
            BackGroundColor.ValueChanged += (p) => ResetBg();

If BackGround change I edit the Background setter:

if (BgSetter != null) Style.Setters.Remove(BgSetter);

BgSetter = null;

if (!BackGroundColor.Value.HasValue) return;

BgSetter = new Setter(GridViewRow.BackgroundProperty, new SolidColorBrush(BackGroundColor.Value.Value));
Style.Setters.Add(BgSetter);

 

I think this is logically correct but Grid Style don't change until a rows refresh occours.

How I can force grid to re-evaluate row stryle rules?

marc.

4 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 11 Dec 2017, 10:35 AM
Hello Marcello,

I'm unsure about the exact setup at your end, but will it be possible for you to set a binding for the Background property of the style and use an IValueConverter to handle the logic, rather than update the style's setters collection? I believe this way the control should correctly reevaluate the style upon value change.

Please let me know if this would indeed be possible. If that is not the case, may I kindly ask you to open a new support ticket and provide a small sample project for me to investigate and get a better understanding of your current setup and requirements?

I look forward to hearing from you.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Marcello
Top achievements
Rank 1
Iron
answered on 11 Dec 2017, 12:44 PM

Hi Dilyan,

 

The problem is that the Style Rules is created dynamically.

You can try this.

In a window add a grid with some rows and a button.

On button click add a StyleRule setting the row background for certain condition (in ex: 2>1)

Look the style is not applied, how to force grid to re-evaluate style rules?

 

0
Dilyan Traykov
Telerik team
answered on 12 Dec 2017, 03:12 PM
Hello Marcello,

Thank you for the clarification.

In such case, you will need to explicitly notify the control that the change has occurred. This can be done by either updating the RowStyleSelector property by setting it to a new instance or calling the OnApplyTemplate method to reevaluate the style selector for each row.

The former approach would be the suggested one, and you can either simply create a new ConditionalStyleSelector instance, create an extension method to clone the ConditionalStyleSelector or derive from the class and create a new method to call the protected MemberwiseClone method and return a new instance.

Please let me know whether any of these approaches would work for you.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Marcello
Top achievements
Rank 1
Iron
answered on 13 Dec 2017, 07:32 AM

Hi,

the second approach works good.I don't tried the first one, but I think it is too verbose for the simple target as set the same ConditionalStyleSelector as a new one...

Thanks

marc.

Tags
GridView
Asked by
Marcello
Top achievements
Rank 1
Iron
Answers by
Dilyan Traykov
Telerik team
Marcello
Top achievements
Rank 1
Iron
Share this question
or