Hello,
I have a problem with the XamDataChart Legend.
Is there a way to create a new line for the Legend items in a XamDataChart Legend, depending on the size of the window? If I make the window smaller, and not all of the items fit in horizontally, can I make them split and show the rest in a new line?
I don't want to use a Scrollbar for this.
Is there a way to implement this?
Best Regards
Hello and thank you for contacting Infragistics.
We're adding an Orientation property to ItemLegend to allow both vertical/horizontal legends, but not wrapping (will be added in a later release) and is considered to be a new product idea.
For now you can re-template the Legend with a custom WrapPanel as demonstrated below, but keep in mind that once we do add support for wrapping this will become obsolete and could interfere with the legend.
public class LegendWrap : WrapPanel { public LegendWrap() { } #region Legend Dependency Property internal const string LegendPropertyName = "Legend"; public static readonly DependencyProperty LegendProperty = DependencyProperty.Register(LegendPropertyName, typeof(Legend), typeof(LegendWrap), new PropertyMetadata( null, (sender, e) => { ((LegendWrap)sender).RaisePropertyChanged(LegendPropertyName, e.OldValue, e.NewValue); })); public Legend Legend { get { return (Legend)this.GetValue(LegendProperty); } set { this.SetValue(LegendProperty, value); } } #endregion private void RaisePropertyChanged(string legendPropertyName, object oldValue, object newValue) { PropertyUpdatedOverride(legendPropertyName, oldValue, newValue); } private void PropertyUpdatedOverride(string propertyName, object oldValue, object newValue) { switch(propertyName) { case LegendPropertyName: if (oldValue != null) { var oldLegend = oldValue as Legend; oldLegend.Children.CollectionChanged -= Legend_ChildrenChanged; } if (newValue != null) { var newLegend = newValue as Legend; newLegend.Children.CollectionChanged += Legend_ChildrenChanged; EnsureInitialContent(); } break; } } private bool _hasInitialContent = false; private void EnsureLegend() { if (Legend == null) { var p = VisualTreeHelper.GetParent(this); while (p != null) { if (p is Legend) { Legend = (Legend)p; break; } p = VisualTreeHelper.GetParent(p); } } } protected override Size MeasureOverride(Size constraint) { EnsureInitialContent(); return base.MeasureOverride(constraint); } private bool EnsureInitialContent() { EnsureLegend(); if (!_hasInitialContent && Legend != null) { _hasInitialContent = true; //full sync foreach (var item in Legend.Children) { ContentControl i = CloneItem((ContentControl)item); Children.Add(i); } return true; } return false; } private void Legend_ChildrenChanged(object sender, NotifyCollectionChangedEventArgs e) { if (EnsureInitialContent()) { return; } switch (e.Action) { case NotifyCollectionChangedAction.Add: if (e.NewItems != null) { for (var i = 0; i < e.NewItems.Count; i++) { ContentControl item = CloneItem((ContentControl)e.NewItems[i]); Children.Insert(i + e.NewStartingIndex, item); } } break; case NotifyCollectionChangedAction.Remove: if (e.OldItems != null) { for (var j = 0; j < e.OldItems.Count; j++) { Children.RemoveAt(e.OldStartingIndex); } } break; case NotifyCollectionChangedAction.Replace: if (e.OldItems != null) { for (var k = 0; k < e.OldItems.Count; k++) { Children.RemoveAt(e.OldStartingIndex); } } if (e.NewItems != null) { for (var m = 0; m < e.NewItems.Count; m++) { ContentControl item = CloneItem((ContentControl)e.NewItems[m]); Children.Insert(m + e.NewStartingIndex, item); } } break; case NotifyCollectionChangedAction.Reset: Children.Clear(); foreach (var item in Legend.Children) { ContentControl i = CloneItem((ContentControl)item); Children.Add(i); } break; } } private ContentControl CloneItem(ContentControl contentControl) { var cc = new ContentControl(); cc.ContentTemplate = contentControl.ContentTemplate; cc.Content = contentControl.Content; return cc; } }
<Style TargetType="ig:Legend" x:Key="WrapLegend"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ig:Legend"> <Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}"> <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> <Grid Margin="{TemplateBinding Padding}"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Rectangle Fill="Transparent" Grid.RowSpan="2"/> <local:LegendWrap x:Name="legendWrap" > </local:LegendWrap> <ContentPresenter x:Name="ContentPresenter" Visibility="Collapsed" /> </Grid> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>