480
Is it possible to create wrapping horizontal legend?
posted

Hi, I have been trying to create xamDataChart with wrapping horizontal legend where legend items show up on multiple lines instead of default behaviour (one line and horizontal scrollbar). I'm using Infragistics version 13.1.

I found similar questions asked here before, and they suggested replacing StackPanel which contains legend items with WrapPanel on datachart's loaded event like this:

private void DataChart_Loaded(object sender, RoutedEventArgs e)
{
   ContentPresenter cp = Utilities.GetDescendantFromName(_legend, "ContentPresenter") as ContentPresenter;
   WrapPanel wp = new WrapPanel() { Orientation = System.Windows.Controls.Orientation.Horizontal };
   StackPanel sp = cp.Content as StackPanel;

   int count = sp.Children.Count;
   for (int i = 0; i < count; i++)
   {
      ContentControl cc = sp.Children[0] as ContentControl;
      sp.Children.Remove(sp.Children[0] as ContentControl);
      wp.Children.Add(cc);
   }

   cp.Content = wp;
}

This was marked as verified answer here, but I get InvalidOperationException from it: "Specified element is already the logical child of another element. Disconnect it first." Is there something I'm missing here, or has this solution deprecated somewhere before version 13.1?

I also tried a different approach to my problem. Here I define own legends for each serie like this:

<ig:XamDataChart>

...

<ig:ColumnSeries Legend="{Binding ElementName=TitleLegend1}" ...

<ig:ColumnSeries Legend="{Binding ElementName=TitleLegend2}" ...

<ig:ColumnSeries Legend="{Binding ElementName=TitleLegend3}" ...

...

</ig:XamDataChart>

<ig:XamDock ig:XamDock.Edge="OutsideBottom" ig:XamDock.HorizontalDockAlignment="Center">
   <WrapPanel>
      <ig:Legend x:Name="TitleLegend1" />

      <ig:Legend x:Name="TitleLegend2" />

      <ig:Legend x:Name="TitleLegend3" />

      ...

   </WrapPanel>

</ig:XamDock>

This works fine and I like that I can style and construct the whole legend area and individual legends by myself. Legend items wrap nicely to multiple rows when needed. Problem is that my chart can also have stacked series:

<ig:StackedColumnSeries Legend="{Binding ElementName=TitleLegend4}" ...
   <ig:StackedColumnSeries.Series>
      <ig:StackedFragmentSeries />
      <ig:StackedFragmentSeries />
      <ig:StackedFragmentSeries />

     ...

   </ig:StackedColumnSeries.Series>

</ig:StackedColumnSeries>

So this stacked serie can have only one legend (TitleLegend4) where it show all fragment series using default legend template (StackPanel and no wrapping). Is it possible to define own legend for each StackedFragmentSeries? It would make this solution to work for my needs.

By the way, does this question form support code formatting like for example StackOverflow? It would make writing and reading questions including code snippets much easier.

Best regards, Henri