c# - Changing color of a single cell in WPF Datagrid -
i have wpf datagrid variable number of columns , populated @ runtime 2d array of string[][].
string[] info = .....; datagrid.itemssource = data;
i need chance cell colors based on values. these values defined user during execution. examples found changing cell colors utilize triggers on xaml , value known in design time.
so i'm changing background of cell this:
datagridcell cell = datagridutils.getcell(datagrid, i, j); cell.background = new solidcolorbrush(colors.darkgreen);
and getcell function:
private datagridcell getcell(int rowindex, int colindex, datagrid dg) { datagridrow row = dg.itemcontainergenerator.containerfromindex(rowindex) datagridrow; datagridcellspresenter p = getvisualchild<datagridcellspresenter>(row); datagridcell cell = p.itemcontainergenerator.containerfromindex(colindex) datagridcell; homecoming cell; }
at first glance seems doing ok, can see cell value in green. if scroll datagrid way downwards , go cell in greenish changes random cell nad more cells turning green. if maintain going , downwards cells in greenish maintain changing randomly.
i know sounds weird, there's 1 place in code changes cell colors, , it's in button click. don't know how happening when scrolling datagrid.
i read somewhere itemcontainergenerator it's not elegant solution , should avoided. it's way managed create work.
is there improve way alter background color of cell? can triggers without knowing values @ design time? if so, how?
datagrid
has virtualizationmode
default recycling
means it'll reuse datacell
generated improve performance. won't have create n times of controls.
add datagrid
virtualizingstackpanel.virtualizationmode="standard"
be aware it'll impact performance , wpf create n times of control.
c# wpf .net-4.5
No comments:
Post a Comment