I know this has been asked a million times, but I haven't been piece together anything that works.
- Coloring cell by cell, not per row
- Fields created in code
- Colors dynamic
A selector didn't seem to do the trick, because simply setting the style erases all other things I set when building the fields.
I've done other binding through triggers, but that was for a single state (bold/not bold), something like
trig.Binding =
new Binding("DataItem.Changed");
trig.Value =
false;
trig.Setters.Add(
new Setter(XamNumericEditor.FontWeightProperty, FontWeights.Normal));
style.Triggers.Add(trig);
trig =
new DataTrigger();
true;
new Setter(XamNumericEditor.FontWeightProperty, FontWeights.Bold));
What I'd like to do is bind to a property on my DataItem that returns a Brush, or should I create a bunch of triggers on a, say, int property with individual Brush values?
Or does anyone have a better idea?
Hello,
So if I understood you correctly, you want to change the individual cell's background color according to the value of that cell (for example INT) ?
If this is the case, according to me, the best approach is to use Converters -- from int to Brush
for example - when value > 10 - Brushes.Red
value = 0 - Brushed.Green
value > 100 - Brushes.Yellow.
You can see example of that on the link below
http://community.infragistics.com/forums/p/20644/80133.aspx#80133
Let me know if that helps and if you are trying to achieve this behavior of the Background and Value of the CellValuePresenter?
Alex.
That looks useful, but:
- The color is based on a property of the backing item, but not the displayed value;
- I need to do this binding in code (I dynamically construct the entire grid). So far, my attempts at setting a binding in code has thrown an exception.
I hope to start tinkering with this later today.
I think that the best way to do this is to set it in the CellValuePresent class' Loaded event. There you'll check for the field that has sent the event and for anything else you might need.
You can create this from the EeventManager in code behind or by using a style's event setter in the xaml.
Hope this helps Petar.
Sounds like a plan. Question #543782548 though (are you sick of me yet?)... I have this in my window's constructor, but the event never fires -- what am I doing wrong?
EventManager.RegisterClassHandler(typeof(CellValuePresenter), CellValuePresenter.LoadedEvent, new RoutedEventHandler(PresenterLoaded), true);
Handler sig
It should be A LoadCompletedEvenntHandler or smth like that.
You'll get it going a lot faster from the xaml like so:
<Window.Resources> <Style TargetType="{x:Type igDP:CellValuePresenter}"> <EventSetter Event="Loaded" Handler="cvp_Loaded"/> </Style></Window.Resources>
And in the code behind:
void cvp_Loaded(object sender, RoutedEventArgs e) { CellValuePresenter cvp = (sender as CellValuePresenter); if (cvp.Field.Name == "Age") { cvp.Background = Brushes.Red; } }
Petar.
That works like a charm in a test project.
Alas, the event doesn't fire in the application I need it in -- obviously, I'm doing something else wrong in this massive form, but that's not your problem.
I want to the hide the checkbox when I change my background to gray -- what do I need to do to make this happen? I have the gray background, just can't find a way to hide the checkbox for that cell.
Using a converter (IValueConverter) to bind the Visibility property of the CheckBox and the Background of the cellvaluepresente does not work? Have you tried that?