I am creating a column series in XamDataChart and the items I am binding to have a value property and a brush property. How can I bind the brush property to the color of the column, so that each column in the series get the color defined in my brush property?
Regards,
Hilma Maria
Hello Hilma Maria,
I have been looking into your question and after investigation found this forum thread where a similar question about customizing the ColumnSeries column colors in the XamDataChart has been discussed. I have also created a small sample application that demonstrates the suggested approach there and sets column fill colors based on their items’ brush property.
To sum up, in order to set the column color based on some item’s property, you need to first set the IsCustomCategoryStyleAllowed property on the ColumnSeries to "True" and handle the AssigningCategoryStyle event on the same series. This event is fired for every column and the corresponding item can be retrieved from the event arguments by calling the GetItems method. The column color can be set by assigning the item’s brush property to the event arguments’ Fill property:
private void ColumnSeries_AssigningCategoryStyle(object sender, Infragistics.Controls.Charts.AssigningCategoryStyleEventArgs args) { SampleData currentColumn = args.GetItems(args.StartIndex, args.EndIndex)[0] as SampleData; Console.WriteLine(currentColumn.Value.ToString() + " " + currentColumn.ColumnBrush.ToString()); args.Fill =currentColumn.ColumnBrush; }
You can find the sample attached below. Please, test it on your side and let me know if I may be of any further assistance.
Sincerely,
Bozhidara Pachilova
Associate Software Developer
1030.XDCColumnSeriesColorBinding.zip
Hello again,
I realized my binding to my command was wrong and changed it to
<interactivity:InvokeCommandAction Command="{Binding DataContext.AssignCategoryStyleCommand, RelativeSource={RelativeSource AncestorType=wpf:View}}"/>
to get the correct DataContext. The event is still not triggered, though. I have tried to change the event to MouseOver and then my command is called (with a crash of course, since the command has wrong argument for that) but my point is that the binding is now correct. For the AssigningCategoryStyle event everything is silent.
Can you see if I am missing something? Or is it not possible to bind the AssigningCategoryStyle to a command?
Thank you for the code-snippet you have provided.
I have been looking into binding the AssigningCategoryStyle event to a command and determined that the event is indeed not fired. What I can suggest instead is using a Behavior. This will allow you to handle the event in a separate file and attach it to the corresponding control and thus maintain the MVVM pattern. You can check out this blog post about Behaviors in WPF, if you are interested.
The behavior can be defined as follows:
public class AssigningCategoryStyleBehavior : Behavior<ColumnSeries> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.AssigningCategoryStyle += AssociatedObject_AssigningCategoryStyle; } private void AssociatedObject_AssigningCategoryStyle(object sender, AssigningCategoryStyleEventArgs args) { SampleData currentColumn = args.GetItems(args.StartIndex, args.EndIndex)[0] as SampleData; args.Fill = currentColumn.ColumnBrush; } }
And attached to the ColumnSeries like this:
<i:Interaction.Behaviors> <local:AssigningCategoryStyleBehavior /> </i:Interaction.Behaviors>
Attached you will find the modified sample for your reference. If you require any further assistance on the matter, please let me know.
8750.XDCColumnSeriesBehavior.zip
Hi Bozhidara,
The behavior solution worked fine, thanks for the help.
One more question - the fact that the AssigningCategoryStyle can not be bound to a command, is it considered a bug and will it be fixed?
I created the following case for you: CAS-210463-N5G6D6 and will update you through it.
You could see it in your account in the 'Support Activity' page.
If you require any further assistance on the matter, please let me know.