Change Silverlight Chart Legend Item Layout

强颜欢笑 提交于 2019-11-29 03:53:37

For completeness sake, here is the LegendStyle="{StaticResource BottomLegendLayout} (very useful tool to figure out styles is the Silverlight Default Style Browser)

        <Style x:Name="BottomLegendLayout" TargetType="dataviz:Legend">
        <Setter Property="BorderBrush" Value="Black" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="chartingToolkit:LegendItem" >
                    <Setter Property="IsTabStop" Value="False" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="chartingToolkit:LegendItem">
                                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="2,2,5,2"> 
                                        <Rectangle Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="1" Margin="0,0,3,0" VerticalAlignment="Center" />
                                        <dataviz:Title Content="{TemplateBinding Content}"  VerticalAlignment="Center"/>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="TitleStyle">
            <Setter.Value>
                <Style TargetType="dataviz:Title">
                    <Setter Property="Margin" Value="0,5,0,10" />
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="HorizontalAlignment" Value="Center" />
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel"> <!-- change layout container for legend items to be horizonal -->
            <Setter.Value>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="dataviz:Legend">
                    <!--  This is the border around the legend area.
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="2">
                    -->
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <!-- Uncomment the next line to show a grid title. -->
                            <!--<dataviz:Title Grid.Row="0" x:Name="HeaderContent" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" Style="{TemplateBinding TitleStyle}"/>-->
                            <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto" BorderThickness="0" Padding="0" IsTabStop="False">  
                                <ItemsPresenter x:Name="Items" /> 
                            </ScrollViewer>
                        </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>       

Adding the following to the chart will do the trick (from here):

<chartingToolkit:Chart.LegendStyle>
    <Style TargetType="dataviz:Legend">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</chartingToolkit:Chart.LegendStyle>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!