binding - Add extra column to WPF DataGrid which is not in the collection -


i'm using datagrid control display content of datatable. therefore data table set itemssource of datagrid.

following columns of datagrid represent columns of data table:

type, name, domain, subdomain

now dynamic column, called "properties" should contain specific information, depending on value of "type" column. this:

switch (type)     case abc:   content="row.field1"     case def:   content="row.field2"     case xyz:   content="row.fieldx" 

where field1 .. fieldx columns data table. i'm using datagrid bindinglistcollectionview. best if solution build on this.

i tried multivalue binding , multivalue converter have more freedom , not having pre-select fields:

var bind = new multibinding();  bind.bindings.add(new binding("protocol")); bind.bindings.add(new binding("path1")); bind.bindings.add(new binding("path2")); bind.bindings.add(new binding("path3")); bind.bindings.add(new binding("path4"));  bind.converter = _configurationmultivalueconverter;  col.binding = bind; 

you use that. of course it's example.

public class yourclass {     public yourtype protocol;     public yourtype2 path1;     public yourtype3 path2;     public yourtype4 path3;     public yourtype5 path4;     public int chooseexpression;     public yourtype6 field1;     public yourtype7 field3;     public yourtype8 fieldx; } 

and in datagrid

<datagrid name="ifyouneedaname" autogenneratecolumn="false" itemssource={binding yourclass} >     <datagrid.columns>         <datagridtextcolumn header="protocolheader" binding={binding protocol} />         <datagridtextcolumn header="path1header" binding={binding path1} />         ...         <datagridtextcolumn header="thechoosenone" binding={binding yourclass, yourbindingconverer} />     </datagrid.columns> </datagrid> 

and @ last converter, inherit ivalueconverter

public class yourbindingconverer : ivalueconverter {     public object convert(object value, type targettype,          object parameter, cultureinfo culture)     {         switch(value.chooseexpression)             case 1: return field1.tostring();             case 2: return field3.tostring();             case 3: return fieldx.tostring();         else             return string.empty;         end;     }      public object convertback(object value, type targettype,          object parameter, cultureinfo culture)     {         throw new notimplementedexception();     } } 

i've wrote memory, becasue don't have vs here. think, that. if have more questions, feel free ask.

and bonus, here solution value converters.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -