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?
Hi Bozhidara,
Thanks for your sample. My solution is strict MVVM though, and I tried to bind the event to a command but the command is never executed. Any clues on why? I am binding events to commands in my xamTreeGrid in the same way without problems. Here is my code, simplified to only show this:
<charts:ColumnSeries ItemsSource="{Binding MyItems}" Title="Max" IsCustomCategoryStyleAllowed="True" ValueMemberPath="Item.Value" XAxis="{Binding ElementName=XAxis}" YAxis="{Binding ElementName=YAxis}" ShowDefaultTooltip="True" IsHighlightingEnabled="True" LegendItemTemplate="{StaticResource LegendItemTemplate}"> <i:Interaction.Triggers> <i:EventTrigger EventName="AssigningCategoryStyle"> <interactivity:InvokeCommandAction Command="{Binding AssignCategoryStyleCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </charts:ColumnSeries>
And in my view model I have:
public MyViewModel()
{
this.AssignCategoryStyleCommand = new DelegateCommand<AssigningCategoryStyleEventArgs>(this.AssignCategoryStyle);
}
public DelegateCommand<AssigningCategoryStyleEventArgs> AssignCategoryStyleCommand { get; }
private void AssignCategoryStyle(AssigningCategoryStyleEventArgs args) {MyItem currentColumn = args.GetItems(args.StartIndex, args.EndIndex)[0] as MyItem;
args.Fill = currentColumn?.Brush; }