如何在xaml中更改silverlight 4图表中每条线的颜色

发布于 2024-11-27 15:49:51 字数 1806 浏览 1 评论 0 原文

如何更改 silverlight 4 工具包图表的 xaml 中每行的颜色?我见过很多代码背后的技巧,但我不想这样做。有没有办法使用 mvvm 来做到这一点?我更喜欢用 xaml 来做。

这是我尝试过的:

<toolkit:Chart Grid.Row="1" Title="Actuals + Forecast" DataContext="{Binding Path=SelectedSKU}">
            <toolkit:Chart.Series>
                <toolkit:LineSeries Title="Manual Forecast"
                                    ItemsSource="{Binding Path=LstChartData}"
                                    IndependentValueBinding="{Binding Path=StrPeriod}"
                                    DependentValueBinding="{Binding Path=DQuantity}" BorderBrush="#FFFF0300"/>
                <toolkit:LineSeries Title="Automatically Generated Forecast"
                                    ItemsSource="{Binding Path=LstAChartData}"
                                    IndependentValueBinding="{Binding Path=StrPeriod}"
                                    DependentValueBinding="{Binding Path=DQuantity}" Foreground="Green"BorderBrush="Green" />
                <toolkit:LineSeries Title="Actual History"
                                    ItemsSource="{Binding Path=LstMChartData}"
                                    IndependentValueBinding="{Binding Path=StrPeriod}"
                                    DependentValueBinding="{Binding Path=DQuantity}" Foreground="Blue" />
            </toolkit:Chart.Series>
        </toolkit:Chart>

我看到这篇文章: http://blogs.msdn.com/b/delay/archive/2009/02/04/columns-of-a- Different-color-customizing-the-appearance-of-silverlight-charts- with-re-templatating-and-mvvm.aspx ,但它说的是 Silverlight 2。我听说它与 Silverlight 4 完全不同。

How do I change the colors of each line in xaml for the silverlight 4 toolkit chart? I have seen a lot of code behind tricks, but I do not want to do that. Is there a way to do it using mvvm? I would prefer to do it in xaml.

This is what I have tried:

<toolkit:Chart Grid.Row="1" Title="Actuals + Forecast" DataContext="{Binding Path=SelectedSKU}">
            <toolkit:Chart.Series>
                <toolkit:LineSeries Title="Manual Forecast"
                                    ItemsSource="{Binding Path=LstChartData}"
                                    IndependentValueBinding="{Binding Path=StrPeriod}"
                                    DependentValueBinding="{Binding Path=DQuantity}" BorderBrush="#FFFF0300"/>
                <toolkit:LineSeries Title="Automatically Generated Forecast"
                                    ItemsSource="{Binding Path=LstAChartData}"
                                    IndependentValueBinding="{Binding Path=StrPeriod}"
                                    DependentValueBinding="{Binding Path=DQuantity}" Foreground="Green"BorderBrush="Green" />
                <toolkit:LineSeries Title="Actual History"
                                    ItemsSource="{Binding Path=LstMChartData}"
                                    IndependentValueBinding="{Binding Path=StrPeriod}"
                                    DependentValueBinding="{Binding Path=DQuantity}" Foreground="Blue" />
            </toolkit:Chart.Series>
        </toolkit:Chart>

I saw this article: http://blogs.msdn.com/b/delay/archive/2009/02/04/columns-of-a-different-color-customizing-the-appearance-of-silverlight-charts-with-re-templating-and-mvvm.aspx , but it says Silverlight 2. I hear it is completely different than Silverlight 4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2024-12-04 15:49:51

弄清楚了:

<toolkit:Chart Grid.Row="1" Title="Actuals + Forecast" DataContext="{Binding Path=SelectedSKU}">
        <toolkit:Chart.Series>
            <toolkit:LineSeries Title="Manual Forecast"
                                ItemsSource="{Binding Path=LstChartData}"
                                IndependentValueBinding="{Binding Path=StrPeriod}"
                                DependentValueBinding="{Binding Path=DQuantity}">
                <toolkit:LineSeries.DataPointStyle>
                    <Style TargetType="toolkit:LineDataPoint">
                        <Setter Property="Background" Value="Lime" />
                        <Setter Property="Template" Value="{x:Null}"/>
                    </Style>
                </toolkit:LineSeries.DataPointStyle>
            </toolkit:LineSeries>
        </toolkit:Chart.Series>
    </toolkit:Chart>

删除实际的点,因此如果您想要该点,请取消该样式。

@Vorrtex,你的解决方案似乎使我的简单要求变得过于复杂。并不是说这很糟糕,只是不是我想要的,但感谢您花时间提供帮助。

Figured it out:

<toolkit:Chart Grid.Row="1" Title="Actuals + Forecast" DataContext="{Binding Path=SelectedSKU}">
        <toolkit:Chart.Series>
            <toolkit:LineSeries Title="Manual Forecast"
                                ItemsSource="{Binding Path=LstChartData}"
                                IndependentValueBinding="{Binding Path=StrPeriod}"
                                DependentValueBinding="{Binding Path=DQuantity}">
                <toolkit:LineSeries.DataPointStyle>
                    <Style TargetType="toolkit:LineDataPoint">
                        <Setter Property="Background" Value="Lime" />
                        <Setter Property="Template" Value="{x:Null}"/>
                    </Style>
                </toolkit:LineSeries.DataPointStyle>
            </toolkit:LineSeries>
        </toolkit:Chart.Series>
    </toolkit:Chart>

the <Setter Property="Template" Value="{x:Null}"/> removes the actual point, so if you want the point take off that style.

@Vorrtex, your solution seems to over complicate my simple requirements. Not that it's bad, but just not what I was looking, but thank you for taking the time to help.

巨坚强 2024-12-04 15:49:51

一种方法是提供一种重新定义图表调色板的样式:

<Style x:Key="newGraphPalette" TargetType="toolkit:Chart">

  <Setter Property="Palette">
     <Setter.Value>
       <toolkit:ResourceDictionaryCollection>
         <!-- set the first line color to red -->
         <ResourceDictionary>
            <SolidColorBrush x:Key="Background" Color="Red"/>
            <Style x:Key="DataPointStyle" TargetType="Control">
               <Setter Property="Background" Value="StaticResource Background"/>
            </Style>
         </ResourceDictionary>

         <!-- set the second line color to green -->
         <ResourceDictionary>
            <SolidColorBrush x:Key="Background" Color="Green"/>
            <Style x:Key="DataPointStyle" TargetType="Control">
               <Setter Property="Background" Value="StaticResource Background"/>
            </Style>
         </ResourceDictionary>


         <!-- set the third line color to blue -->
         <ResourceDictionary>
            <SolidColorBrush x:Key="Background" Color="Blue"/>
            <Style x:Key="DataPointStyle" TargetType="Control">
               <Setter Property="Background" Value="StaticResource Background"/>
            </Style>
         </ResourceDictionary>
       </toolkit:ResourceDictionaryCollection>
     </Setter.Value>
  </Setter>

</Style>

当您想使用此特定调色板时 - 使用

Style="StaticResource newGraphPalette"

这将覆盖图表的默认调色板,并且您只需在资源中定义颜色(样式)可以重复使用。

希望这会有所帮助。

One way is to provide a style that redefines a Palette for the graph:

<Style x:Key="newGraphPalette" TargetType="toolkit:Chart">

  <Setter Property="Palette">
     <Setter.Value>
       <toolkit:ResourceDictionaryCollection>
         <!-- set the first line color to red -->
         <ResourceDictionary>
            <SolidColorBrush x:Key="Background" Color="Red"/>
            <Style x:Key="DataPointStyle" TargetType="Control">
               <Setter Property="Background" Value="StaticResource Background"/>
            </Style>
         </ResourceDictionary>

         <!-- set the second line color to green -->
         <ResourceDictionary>
            <SolidColorBrush x:Key="Background" Color="Green"/>
            <Style x:Key="DataPointStyle" TargetType="Control">
               <Setter Property="Background" Value="StaticResource Background"/>
            </Style>
         </ResourceDictionary>


         <!-- set the third line color to blue -->
         <ResourceDictionary>
            <SolidColorBrush x:Key="Background" Color="Blue"/>
            <Style x:Key="DataPointStyle" TargetType="Control">
               <Setter Property="Background" Value="StaticResource Background"/>
            </Style>
         </ResourceDictionary>
       </toolkit:ResourceDictionaryCollection>
     </Setter.Value>
  </Setter>

</Style>

And when you want to use this particular palette - use the

Style="StaticResource newGraphPalette"

this will over write the default palette of the chart, and you only have to define the colors ones in a resource (style) that can be reused.

Hopefully this is helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文