I am trying write a template to Legend control. I have version 10.3, .net 4, xamDataChart, with a few line series.
My Legend is Horizontal and I want this to wrap when i have many items(series). Something thing like tab control showing multiple lines. Is this possible? I have to have my Legend at the Top.
I have custom Legend template and Legendtemplateitem, but it's not working. I just comented the ScrowViewer and replace this for a WrapPanel.
Any Help is welcome.
<ControlTemplate x:Key="xamDataChartLegendTemplate" TargetType ="Charts:Legend">
<Grid>
<Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="4" Background="{TemplateBinding Background}">
<Grid Margin="{TemplateBinding Padding }">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" Content="{TemplateBinding Content}" />
<!--<ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="auto" VerticalScrollBarVisibility="auto" BorderThickness="0" Background="Transparent">-->
<WrapPanel Orientation="Horizontal" Grid.Row="1">
<ContentPresenter Grid.Row="1" x:Name="ContentPresenter" />
</WrapPanel>
<!--</ScrollViewer>-->
</Grid >
</Border>
<Border IsHitTestVisible="False" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="Transparent" CornerRadius="4" Background="{StaticResource GenericOverlay}" />
</Grid>
</ControlTemplate>
<
DataTemplate x:Key="xamDataChartCustomLegendItemTemplate">
.Visibility}">
.LegendItemBadgeTemplate}" />
}"/>
Hello,
Thank you for your post. I have been looking into it and I modified the sample project, so now it uses version 11.2 and everything seems to work ok on my side. If the sample doesn’t satisfies all your needs feel free to modify it, so it reproduces your behavior and send it back to me for further investigation.
Looking forward for your reply.
This is my leged template:
<ControlTemplate x:Key="xamDataChartLegendTemplate" TargetType="Charts:Legend">
<Grid Margin="{TemplateBinding Padding}">
<ContentPresenter x:Name="ContentPresenter" />
<Border IsHitTestVisible="False" BorderThickness="{TemplateBinding BorderThickness}"BorderBrush="Transparent" CornerRadius="4" Background="{StaticResource GenericOverlay}" />
<Border IsHitTestVisible="False" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="Transparent" CornerRadius="4" Background="{StaticResource GenericOverlay}" />
Hi Stefan,
I just upgrade this application from version 10.3 to 11.2.
The code still working but when I try dock the pane with the chart, i got this exception: "Specified element is already the logical child of another element. Disconnect it first."
If I comment out this code below, it works.
Can you help me with that?
This is my code:
private void MainDataChart_Loaded(object sender, RoutedEventArgs e) { // Change Legend to show multiple lines ContentPresenter cp = Utilities.GetDescendantFromName(xmLegend, "ContentPresenter") as ContentPresenter; if (cp != null) { StackPanel sp = cp.Content as StackPanel; if (sp != null) { WrapPanel wp = new WrapPanel() {Orientation = System.Windows.Controls.Orientation.Horizontal}; 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); if (cc != null) wp.Children.Add(cc); } cp.Content = wp; } } }
I can say that the Legend is separate element from the XamDataChart. In order to make the Legend render over the chart you have to add it to the Visual Tree after you add the chart and I can suggest you add the Chart and the Legend in a DockManager or Grid, so you can organize them as you want. In the Feature Browser under xamDataChart / Display – Legends / Legend Docking section there is a sample demonstrating how to place the Legend.
Hope this helps you.