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

本文关键字:背景 红色 系列 DVC | 更新日期: 2023-09-27 18:19:44

嗨,我正在下载Microsoft wpftoolkit,使用图表控件绘制columnsseries图表。我可以使用我的数据绘制图表,但条形图的背景颜色没有改变。如何将条形图颜色更改为红色,而不是默认的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>

有人能告诉我怎么做吗?。

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

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

为了解决这个问题,您可以覆盖样式。

<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>

希望这能帮助你。。。

只在DataPointStyle中设置Background属性也可以。

资源:

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

用法:

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