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 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.
Sincerely,
Bozhidara Pachilova
Associate Software Developer
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?
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
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?
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; }