c# - How can I set a property on a Converter object at runtime -


i have datagrid list of dates , values (temperatures). have valueconverter object colors background of each row if temperature high or low. code works fine:

xaml:

<style x:key="graphtabledatarowstyle" targettype="{x:type datagridrow}">     <setter property="background" value="{binding value, converter={staticresource datagridalarmconverter}}"/> </style> 

c#: converter

public class datagridalarmconverter : ivalueconverter {     public double? hialarm = null;     public double? loalarm = 20;     public brush hialarmcolor = brushes.red;     public brush loalarmcolor = brushes.blue;     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         double? input = value double?;          if (input != null)         {             if (hialarm != null)             {                 if (input > hialarm)                 {                     return hialarmcolor;                 }             }             if (loalarm != null)             {                 if (input < loalarm)                 {                     return loalarmcolor;                 }             }         }         return dependencyproperty.unsetvalue;     }     public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notsupportedexception();     } } 

c#: datagrid creation

... datagrid dgrid = new datagrid(); dgrid.style = findresource("graphtabledatagridstyle") style; dgrid.rowstyle = findresource("graphtabledatarowstyle") style; dgrid.itemssource = dsf.boundeddata; ... 

it correctly changes background color of every row temperature less 20.

now want change hialarm , loalarm values dynamically @ run time. if create style in code behind binding converter, this:

datagridalarmconverter dac = new datagridalarmconverter(); dac.hialarm = highalarm; //public property in code behind object dac.loalarm = lowalarm; //use object somehow in style 

but don't know how attach converter object style in code behind.

alternatively, thought of passing code behind object converter property since class contains highalarm , lowalarm properties need:

<style x:key="graphtabledatarowstyle" targettype="{x:type datagridrow}">     <setter property="background" value="{binding value, converter={staticresource datagridalarmconverter}, converterproperty=this}"/> </style> 

i change converter code check properties of object extract highalarm , lowalarm values.

but there no "this" reference in xaml don't know how either.

so question is, how can add converter style in code behind or how can pass parent object converter @ in xaml.

ed plunkett put me on trail of solution. googled how add dynamic resource , stumbled across code did needed.

i determined create binding converter in style in code behind this:

datagridalarmconverter dac = new datagridalarmconverter(); dac.hialarm = highalarm; dac.loalarm = lowalarm; style rowstyle = new style(typeof(datagridrow)); rowstyle.setters.add(new setter(datagridrow.backgroundproperty, new binding()         {             converter = dac,              path = new propertypath("value")         })); 

i used new rowstyle instead of xaml one.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -