i have data grid this:
<datagrid autogeneratecolumns="false" canuserdeleterows="true" horizontalscrollbarvisibility="hidden" margin="10,10,10,10" verticalscrollbarvisibility="visible" canuseraddrows="false" itemssource="{binding listgestores}" tooltip="selecione uma linha e pressione delete para remover uma unidade."> <datagrid.columns> <datagridtextcolumn binding="{binding tb_unidade.ds_nome_unidade}" canuserresize="false" header="setor" isreadonly="true" x:name=""/> <datagridtextcolumn binding="{binding tb_unidade.tb_unidade2.ds_nome_unidade}" canuserresize="false" header="unidade" isreadonly="true" x:name=""/> <datagridtextcolumn binding="{binding tb_contatos.ds_nome}" canuserresize="false" header="gestor" isreadonly="true" /> </datagrid.columns> </datagrid>
the problem need verify if 2 column binding null, , if null, need hide it, , change header of column 1.
i know can triggers, how exactly??? started with:
<datagrid.triggers> <datatrigger binding="{binding tb_unidade.tb_unidade2}" value="{x:null}"> <setter property="" value="" /> </datatrigger> </datagrid.triggers>
but don't know setter or whatever put !
help me =]
example:
with following viewmodel:
public class viewmodel : inotifypropertychanged { public string data1 { get; set; } public string data2 { get; set; } public string data3 { get; set; } }
you can use following sample xaml:
<grid.resources> <local:datagridcolumnvisibilityconverter x:key="datagridcolumnvisibilityconverter" /> </grid.resources> <datagrid> <datagrid.columns> <datagridtextcolumn binding="{binding data1}" visibility="{binding, converter={staticresource datagridcolumnvisibilityconverter}, converterparameter=data1}" /> <datagridtextcolumn binding="{binding data2}" visibility="{binding, converter={staticresource datagridcolumnvisibilityconverter}, converterparameter=data2}" /> <datagridtextcolumn binding="{binding data3}" visibility="{binding, converter={staticresource datagridcolumnvisibilityconverter}, converterparameter=data3}" /> </datagrid.columns> </datagrid>
and converter:
public class datagridcolumnvisibilityconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { viewmodel viewmodel = (viewmodel)value; switch (parameter.tostring().tolower()) { case "data1": return (!string.isnullorempty(viewmodel.data1)) ? visibility.visible : visibility.collapsed; case "data2": return (!string.isnullorempty(viewmodel.data2)) ? visibility.visible : visibility.collapsed; case "data3": return (!string.isnullorempty(viewmodel.data3)) ? visibility.visible : visibility.collapsed; } return visibility.visible; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
Comments
Post a Comment