C#/WPF - Shared Style Setters -
i'm trying style set of controls display levels device.
i'm using label control has style multidata trigger checks if value of field between value, , applies highlight indicate error.
<style x:key="highlight-stat" targettype="label"> <style.triggers> <multidatatrigger> <multidatatrigger.conditions> <condition value="true"> <condition.binding> <multibinding converter="{staticresource betweenvaluesconverter}"> <binding/> <binding> <binding.source> <sys:int32>100</sys:int32> </binding.source> </binding> <binding> <binding.source> <sys:int32>200</sys:int32> </binding.source> </binding> <binding> <binding.source> <sys:boolean>true</sys:boolean> </binding.source> </binding> <binding> <binding.source> <sys:boolean>false</sys:boolean> </binding.source> </binding> </multibinding> </condition.binding> </condition> </multidatatrigger.conditions> <multidatatrigger.setters> <setter property="background" value="red"/> <setter property="foreground" value="white"/> <setter property="fontweight" value="bold"/> </multidatatrigger.setters> </style.triggers> </style>
the issue i'm trying tackle there typically 2 or 3 states: normal, warning, , error. these states apply multiple fields.
i want able consolidate setters of trigger static resource can store separately, list, , use list value multidatatrigger.setters. in way, can define error , warning state collection of setters , update them central location.
ie:
<style x:key="error-state" targettype="control"> <setter property="foreground" value="red"/> <setter property="background" value="white"/> <setter property="fontweight" value="bold"/> </style> <style x:key="warning-state" targettype="control"> <setter property="background" value="yellow"/> </style>
the issue i'm having datatrigger/multidatatrigger's setters property has no set method, , trigger required determine state.
is there way can accomplish want?
possible solutions:
- a control accepts list of ranges, , applies correct style
- copy/paste setters 1 trigger *currently using this*
- @pieterwitvoet recommended
styleselector
edit
i've created user control accomplish want. c#
public class label : system.windows.controls.label, inotifypropertychanged { public event propertychangedeventhandler propertychanged; public label() { var metadata = contentproperty.getmetadata(this); contentproperty.overridemetadata(typeof(label), new frameworkpropertymetadata(propertychanged)); rules = new list<highlightingrule>(); propertychanged += (o, a) => { system.diagnostics.debug.writeline(string.format("#triggered - {0}", a.propertyname)); }; } /// <summary> /// gets or sets highlighting rules applied control /// </summary> public list<highlightingrule> rules { get; set; } static void propertychanged(dependencyobject o, dependencypropertychangedeventargs args) { if (o label) { (o label).reapplyrules(); } } void reapplyrules() { style = null; foreach (var rule in rules) { if (rule.ismatch(content)) { style = rule.matchstyle; break; } } } }
i'm using below base class define rules
/// <summary> /// base class highlighting rules used highlightedlabel /// </summary> public abstract class highlightingrule { public style matchstyle { get; set; } public virtual bool ismatch(object value) { return false; } }
for example:
public class undervaluerule : highlightingrule { public double value { get; set; } public bool inclusive { get; set; } public override bool ismatch(object value) { if (value int || value double) { double dvalue = convert.todouble(value); return dvalue.compareto(value) <= (inclusive ? 0 : -1); } else if (value decimal) { decimal dvalue = convert.todecimal(value); return dvalue.compareto(convert.todecimal(value)) <= (inclusive ? 0 : -1); } return false; } }
in xaml:
<rules:undervaluerule value="31" inclusive="false" matchstyle="{staticresource error-state}"/>
Comments
Post a Comment