【问题标题】:Silverlight DataVisualization Pie Charts - Slice Selection color changeSilverlight DataVisualization 饼图 - 切片选择颜色变化
【发布时间】:2014-03-11 11:56:07
【问题描述】:

我正在开发 Windows Phone 8 应用程序。

我有饼图的工作模型:

以下是点击前的截图:

下面是点击蓝色切片后的截图:

我在这里遇到一个问题,当我选择饼切片时,切片的颜色正在改变,但我不想改变切片的颜色。我怎样才能做到这一点?

我尝试使用 blend 来改变它,但它没有向我显示状态。

这是我的代码:

<charting:Chart
                Height="400"
                Style="{StaticResource PhoneChartStyle}"
                Template="{StaticResource PhoneChartPortraitTemplate}"
                x:Name="MyPieSeriesChart">
                <!-- Series -->
                <charting:PieSeries
                    Tap="PieSeries_Tap"
                    DoubleTap="PieSeries_DoubleTap"
                    IsSelectionEnabled="True"
                    ItemsSource="{StaticResource Activities}"
                    DependentValuePath="Count"
                    IndependentValuePath="Activity">
                    <charting:PieSeries.LegendItemStyle>
                        <Style TargetType="charting:LegendItem">
                            <Setter Property="Margin" Value="5 0 15 0"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Style>
                    </charting:PieSeries.LegendItemStyle>
                </charting:PieSeries>
            </charting:Chart>

PhoneChartStyle 和 PhoneChartPortraitTemplate 代码;

<ControlTemplate x:Key="PhoneChartPortraitTemplate" TargetType="charting:Chart">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <datavis:Title
                Content="{TemplateBinding Title}"
                Style="{TemplateBinding TitleStyle}"/>
            <datavis:Legend x:Name="Legend"
                Grid.Row="2"
                Header="{TemplateBinding LegendTitle}"
                Style="{TemplateBinding LegendStyle}">
                <datavis:Legend.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </datavis:Legend.ItemsPanel>
                <datavis:Legend.Template>
                    <ControlTemplate TargetType="datavis:Legend">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <ScrollViewer
                                    Grid.Row="1"
                                    HorizontalScrollBarVisibility="Auto"
                                    VerticalScrollBarVisibility="Disabled"
                                    BorderThickness="0"
                                    Padding="0"
                                    IsTabStop="False">
                                <ItemsPresenter
                                        x:Name="Items"
                                        Margin="10,0,10,10"/>
                            </ScrollViewer>
                        </Grid>
                    </ControlTemplate>
                </datavis:Legend.Template>
            </datavis:Legend>
            <chartingprimitives:EdgePanel
                Grid.Column="0"
                Grid.Row="1"
                x:Name="ChartArea"
                Style="{TemplateBinding ChartAreaStyle}">
                <Grid
                    Canvas.ZIndex="-1"
                    Style="{TemplateBinding PlotAreaStyle}" />
            </chartingprimitives:EdgePanel>
        </Grid>
    </ControlTemplate>

    <!-- Chart Style for Phone -->
    <Style x:Key="PhoneChartStyle" TargetType="charting:Chart">
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Padding" Value="10" />
        <Setter Property="Palette">
            <Setter.Value>
                <datavis:ResourceDictionaryCollection>
                    <!-- Blue -->
                    <ResourceDictionary>
                        <SolidColorBrush x:Key="Background" Color="#E85F3D"/>
                        <Style x:Key="DataPointStyle" TargetType="Control">
                            <Setter Property="Background" Value="{StaticResource Background}" />
                        </Style>
                        <Style x:Key="DataShapeStyle" TargetType="Shape">
                            <Setter Property="Stroke" Value="{StaticResource Background}" />
                            <Setter Property="StrokeThickness" Value="2" />
                            <Setter Property="StrokeMiterLimit" Value="1" />
                            <Setter Property="Fill" Value="{StaticResource Background}" />
                        </Style>
                    </ResourceDictionary>
                    <!-- Red -->
                    <ResourceDictionary>
                        <SolidColorBrush x:Key="Background" Color="#76D164"/>
                        <Style x:Key="DataPointStyle" TargetType="Control">
                            <Setter Property="Background" Value="{StaticResource Background}" />
                        </Style>
                        <Style x:Key="DataShapeStyle" TargetType="Shape">
                            <Setter Property="Stroke" Value="{StaticResource Background}" />
                            <Setter Property="StrokeThickness" Value="2" />
                            <Setter Property="StrokeMiterLimit" Value="1" />
                            <Setter Property="Fill" Value="{StaticResource Background}" />
                        </Style>
                    </ResourceDictionary>
                    <!-- Light Green -->
                    <ResourceDictionary>
                        <SolidColorBrush x:Key="Background" Color="#648ED1"/>
                        <Style x:Key="DataPointStyle" TargetType="Control">
                            <Setter Property="Background" Value="{StaticResource Background}" />
                        </Style>
                        <Style x:Key="DataShapeStyle" TargetType="Shape">
                            <Setter Property="Stroke" Value="{StaticResource Background}" />
                            <Setter Property="StrokeThickness" Value="2" />
                            <Setter Property="StrokeMiterLimit" Value="1" />
                            <Setter Property="Fill" Value="{StaticResource Background}" />
                        </Style>
                    </ResourceDictionary>
                </datavis:ResourceDictionaryCollection>
            </Setter.Value>
        </Setter>
        <Setter Property="LegendStyle">
            <Setter.Value>
                <Style TargetType="datavis:Legend">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
                    <Setter Property="Margin" Value="20"/>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="ChartAreaStyle">
            <Setter.Value>
                <Style TargetType="Panel">
                    <Setter Property="MinWidth" Value="100" />
                    <Setter Property="MinHeight" Value="75" />
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="PlotAreaStyle">
            <Setter.Value>
                <Style TargetType="Grid">
                    <Setter Property="Background" Value="Transparent"/>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template" Value="{StaticResource PhoneChartPortraitTemplate}"/>
    </Style>

【问题讨论】:

    标签: c# wpf silverlight charts windows-phone


    【解决方案1】:

    您应该编辑您的DataPointStyle(在Visual Studio 中单击鼠标右键-> Edit Additional Templates -&gt; Edit DataPointStyle -&gt; Edit a copy),在下面找到此代码并从Selected 状态中删除Storyboard

    <VisualStateGroup x:Name="SelectionStates">
        <VisualStateGroup.Transitions>
            <VisualTransition GeneratedDuration="0:0:0.1"/>
        </VisualStateGroup.Transitions>
        <VisualState x:Name="Unselected"/>
        <VisualState x:Name="Selected">
            <!-- This storyboard should be removed -->
            <!--<Storyboard>
                <DoubleAnimation
                    Storyboard.TargetName="SelectionHighlight"
                    Storyboard.TargetProperty="Opacity"
                    To="0.6"
                    Duration="0"/>
            </Storyboard>-->
        </VisualState>
    </VisualStateGroup>
    

    然后更新它的名字

    <Style x:Name="NewDataPointStyle" TargetType="charting:PieDataPoint">
    

    并将其设置为图表的数据点。然后,当所有切片都具有相同的颜色(橙色)时,您可能会遇到错误。如果 windows 8 还没有修复这个 bug,你可以使用你自己的调色板,就像我在 this answer about chart colors 中解释的那样

    我看到你有Palette 属性的代码,所以你应该像这样更改它,我添加了BasedOn="{StaticResource NewDataPointStyle}" 并更改了TargetType="charting:PieDataPoint"

    <Setter Property="Palette">
    <Setter.Value>
    <datavis:ResourceDictionaryCollection>
        <!-- Blue -->
        <ResourceDictionary>
            <SolidColorBrush x:Key="Background" Color="#E85F3D"/>
            <Style x:Key="DataPointStyle" TargetType="charting:PieDataPoint" BasedOn="{StaticResource NewDataPointStyle}">
                <Setter Property="Background" Value="{StaticResource Background}" />
            </Style>
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多