c# - Datagrid foreground colour not working -
i have simple datagrid display bidimensional data. have tried in test project , result nice.
here xmal:
<grid > <datagrid name="dg" margin="50" fontsize="26" celleditending="dg_celleditending" beginningedit="dg_beginningedit" loadingrow="datagrid_loadingrow" enablerowvirtualization="false" autogeneratingcolumn="dg_autogeneratingcolumn"/> </grid>
and relevant event code:
private void datagrid_loadingrow(object sender, system.windows.controls.datagridroweventargs e) { e.row.header = "r" + ((e.row.getindex()) + 1).tostring(); } private void dg_autogeneratingcolumn(object sender, system.windows.controls.datagridautogeneratingcolumneventargs e) { string str = e.propertyname; int num = int.parse(e.propertyname); e.column.header = "c" + (num + 1).tostring(); }
then have put in real project styled window.
so put same easy xaml (adding background , foreground fore here have gradient background) xamls is:
<grid > <datagrid name="dtgnests" margin="50" fontsize="26" background="white" foreground="black" headersvisibility="all" celleditending=" dg_celleditending" beginningedit="dg_beginningedit" loadingrow="datagrid_loadingrow" enablerowvirtualization="false" autogeneratingcolumn="dg_autogeneratingcolumn"/> </grid>
and effect is:
so in short foreground not being set in part. is:
- cells
- row headers
- column headers
so nowhere. can tell me why? thank in advance patrick
in wpf datagrid, cell-related design needs set column's elementstyle
, overrides foreground set in grid. try following:
in xaml resources:
<style x:key="blackcellstyle" targettype="{x:type textblock}"> <setter property="foreground" value="black" /> </style>
in autogeneratingcolumn
handler:
private void dg_autogeneratingcolumn(object sender, system.windows.controls.datagridautogeneratingcolumneventargs e) { string str = e.propertyname; int num = int.parse(e.propertyname); e.column.header = "c" + (num + 1).tostring(); e.column.elementstyle = findresource("blackcellstyle") style; }
this apply foreground directly cells
Comments
Post a Comment