如何将 DVC:Chart 中的列系列的条形背景更改为红色?

发布于 2024-12-25 13:00:53 字数 1411 浏览 1 评论 0原文

您好,我正在通过下载 Microsoft wpftoolkit 使用图表控件绘制列系列图表。 我可以使用我的数据绘制图表,但条形图的背景颜色没有改变。如何将条形图颜色更改为红色而不是默认的 LightSteelBlue 颜色。 这是我的代码

<Window x:Class="net.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
Title="Window1" Height="800" Width="800" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<Grid>
       <DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart"
        Width="800" Height="450" FontSize="12"
        Background="DarkGray" Foreground="DarkRed">
        <DVC:Chart.Series>
            <DVC:ColumnSeries x:Name="Barchart" Title="Students"
               ItemsSource="{Binding list}" 
               IndependentValueBinding="{Binding Path=Name}"
               DependentValueBinding="{Binding Path=students}" >
            </DVC:ColumnSeries> 
        </DVC:Chart.Series>
     </DVC:Chart>
</Grid>

,有人可以告诉我如何做到这一点吗?

提前致谢。 请回答这个问题。

Hi i'm plotting columnseries graph using chart controls by downloading Microsoft wpftoolkit.
I can able to draw the graph using my data but the background color of the bar was not changing.How to change the bar color to red instead of default LightSteelBlue color.
Here is my code

<Window x:Class="net.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
Title="Window1" Height="800" Width="800" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<Grid>
       <DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart"
        Width="800" Height="450" FontSize="12"
        Background="DarkGray" Foreground="DarkRed">
        <DVC:Chart.Series>
            <DVC:ColumnSeries x:Name="Barchart" Title="Students"
               ItemsSource="{Binding list}" 
               IndependentValueBinding="{Binding Path=Name}"
               DependentValueBinding="{Binding Path=students}" >
            </DVC:ColumnSeries> 
        </DVC:Chart.Series>
     </DVC:Chart>
</Grid>

can any one tel me how to do this?.

Thanks in advance.
Please answer this.

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

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

发布评论

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

评论(2

花之痕靓丽 2025-01-01 13:00:53

为了解决这个问题,你可以重写样式。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">

  <!-- Resource dictionary entries should be defined here. -->
  <Style x:Key="MyColumnDataPointStyle"
         TargetType="{x:Type chartingToolkit:ColumnDataPoint}">
    <Setter Property="Background"
            Value="Red" />
    <Setter Property="BorderBrush"
            Value="Black" />
    <Setter Property="BorderThickness"
            Value="1" />
    <Setter Property="IsTabStop"
            Value="False" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type chartingToolkit:ColumnDataPoint}">

          <Border x:Name="Root"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
            <Border.ToolTip>
              <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
            </Border.ToolTip>
            <Grid Background="{TemplateBinding Background}">
              <Rectangle>
                <Rectangle.Fill>
                  <LinearGradientBrush>
                    <GradientStop Color="#77FFFFFF"
                                  Offset="0" />
                    <GradientStop Color="Transparent"
                                  Offset="1" />
                  </LinearGradientBrush>
                </Rectangle.Fill>
              </Rectangle>
              <Border BorderBrush="#CCFFFFFF"
                      BorderThickness="1">
                <Border BorderBrush="#77FFFFFF"
                        BorderThickness="1" />
              </Border>
              <Rectangle x:Name="SelectionHighlight"
                         Fill="Red"
                         Opacity="0" />
              <Rectangle x:Name="MouseOverHighlight"
                         Fill="White"
                         Opacity="0" />
            </Grid>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

使用

<Grid>
  <DVC:Chart Canvas.Top="80"
             Canvas.Left="10"
             Name="mcChart"
             Width="800"
             Height="450"
             FontSize="12"
             Background="DarkGray"
             Foreground="DarkRed">
    <DVC:Chart.Series>
      <DVC:ColumnSeries x:Name="Barchart"
                        Style="{StaticResource MyColumnDataPointStyle}"
                        Title="Students"
                        ItemsSource="{Binding list}"
                        IndependentValueBinding="{Binding Path=Name}"
                        DependentValueBinding="{Binding Path=students}">
      </DVC:ColumnSeries>
    </DVC:Chart.Series>
  </DVC:Chart>
</Grid>

希望这对您有帮助...

In order to solve this problem, you can override the style.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">

  <!-- Resource dictionary entries should be defined here. -->
  <Style x:Key="MyColumnDataPointStyle"
         TargetType="{x:Type chartingToolkit:ColumnDataPoint}">
    <Setter Property="Background"
            Value="Red" />
    <Setter Property="BorderBrush"
            Value="Black" />
    <Setter Property="BorderThickness"
            Value="1" />
    <Setter Property="IsTabStop"
            Value="False" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type chartingToolkit:ColumnDataPoint}">

          <Border x:Name="Root"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
            <Border.ToolTip>
              <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
            </Border.ToolTip>
            <Grid Background="{TemplateBinding Background}">
              <Rectangle>
                <Rectangle.Fill>
                  <LinearGradientBrush>
                    <GradientStop Color="#77FFFFFF"
                                  Offset="0" />
                    <GradientStop Color="Transparent"
                                  Offset="1" />
                  </LinearGradientBrush>
                </Rectangle.Fill>
              </Rectangle>
              <Border BorderBrush="#CCFFFFFF"
                      BorderThickness="1">
                <Border BorderBrush="#77FFFFFF"
                        BorderThickness="1" />
              </Border>
              <Rectangle x:Name="SelectionHighlight"
                         Fill="Red"
                         Opacity="0" />
              <Rectangle x:Name="MouseOverHighlight"
                         Fill="White"
                         Opacity="0" />
            </Grid>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

usage

<Grid>
  <DVC:Chart Canvas.Top="80"
             Canvas.Left="10"
             Name="mcChart"
             Width="800"
             Height="450"
             FontSize="12"
             Background="DarkGray"
             Foreground="DarkRed">
    <DVC:Chart.Series>
      <DVC:ColumnSeries x:Name="Barchart"
                        Style="{StaticResource MyColumnDataPointStyle}"
                        Title="Students"
                        ItemsSource="{Binding list}"
                        IndependentValueBinding="{Binding Path=Name}"
                        DependentValueBinding="{Binding Path=students}">
      </DVC:ColumnSeries>
    </DVC:Chart.Series>
  </DVC:Chart>
</Grid>

hope this helps you...

神回复 2025-01-01 13:00:53

仅在 DataPointStyle 中设置背景属性也可以。

Resource:

<Style x:Key="RedColumnDataPointStyle"
       TargetType="{x:Type DVC:ColumnDataPoint}">
    <Setter Property="Background" Value="Red" />
</Style>

Usage:

  <DVC:ColumnSeries x:Name="Barchart"
                    DataPointStyle="{StaticResource RedColumnDataPointStyle}"
                    Title="Students"
                    ItemsSource="{Binding list}"
                    IndependentValueBinding="{Binding Path=Name}"
                    DependentValueBinding="{Binding Path=students}">
  </DVC:ColumnSeries>

Setting just the Background property in the DataPointStyle will also work.

Resource:

<Style x:Key="RedColumnDataPointStyle"
       TargetType="{x:Type DVC:ColumnDataPoint}">
    <Setter Property="Background" Value="Red" />
</Style>

Usage:

  <DVC:ColumnSeries x:Name="Barchart"
                    DataPointStyle="{StaticResource RedColumnDataPointStyle}"
                    Title="Students"
                    ItemsSource="{Binding list}"
                    IndependentValueBinding="{Binding Path=Name}"
                    DependentValueBinding="{Binding Path=students}">
  </DVC:ColumnSeries>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文