c# - Binding a Datagrid Columnheader Backgroud to da datatable row -
i want color datagrid columnheader depending on row in datatable bound datagrid. how can this? method have used on datagrid.row not work, because there apparently no "column" property can ue.
i came this, can usethe whole datagrid in colorconverter, unable find way determine columnheader rendered.
xaml:
<datagrid x:name="exceldatatable_exceldata"> <style targettype="datagridcolumnheader"> <setter property="background" value="{binding relativesource={relativesource ancestortype=datagrid}, converter={staticresource excelcolumncolorconverter}}"></setter> </style> </datagrid>
code behind colorconverter:
public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { var col = (system.data.datacolumn)value; var colstateobj = col.table.rows[col.table.rows.count-1][col.columnname]; enums.rowstate colorvalue = (enums.rowstate)enum.parse(typeof(enums.rowstate), colstateobj.tostring()); switch (colorvalue) { case enums.rowstate.headerrow: return brushes.gainsboro; case enums.rowstate.isincluded: return brushes.lightgreen; case enums.rowstate.notincluded: return brushes.lightsalmon; default: return brushes.azure; } } /// <summary> /// converts value of hidden color row color on data table. /// </summary> /// <param name="value">the value</param> /// <param name="targettype">the type of binding target.</param> /// <param name="parameter">the converter parameter.</param> /// <param name="culture">the culture</param> /// <returns> /// ethe converted value or null /// </returns> /// <exception cref="system.notimplementedexception">not implemented because not needed</exception> public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); }
i have foubnd solution, using multibinding:
xaml:
<style targettype="datagridcolumnheader"> <setter property="background"> <setter.value> <multibinding converter="{staticresource excelcolumncolorconverter}"> <binding relativesource="{relativesource ancestortype=datagrid}"></binding> <binding relativesource="{relativesource self}" path="column"></binding> </multibinding> </setter.value> </setter> </style>
the converter:
public class excelcolumncolorconverter : imultivalueconverter { public object convert(object[] value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (!(value[1] == null)) { var dgrid = (datagrid)value[0]; var colheader = ((datagridtextcolumn)value[1]).header.tostring(); var dview = (system.data.dataview)dgrid.itemssource; var table = dview.table; var rowstateobj = table.rows[table.rows.count - 1][colheader]; enums.rowstate colorvalue = (enums.rowstate)enum.parse(typeof(enums.rowstate), rowstateobj.tostring()); switch (colorvalue) { case enums.rowstate.headerrow: return brushes.gainsboro; case enums.rowstate.isincluded: return brushes.lightgreen; case enums.rowstate.notincluded: return brushes.lightsalmon; default: return brushes.azure; } } else { return null; } }
Comments
Post a Comment