WPF图表删除渐变填充

时间:2016-07-27 08:55:54

标签: c# wpf xaml

我目前正在开发一个简单的c#WPF程序,它必须显示一个柱形图。但是,System.Windows.Controls.DataVisualization.Charting命名空间的基本柱形图带有渐变列填充颜色,我不想在程序中使用它。

有没有办法在xaml或c#代码中将列的填充画笔设置为SolidBrush? 在程序运行时以编程方式添加ColumnSeries,因此我无法在xaml中访问它们。我对WPF和xaml还不熟悉,所以我真的不知道从哪里开始寻找答案

这是我的代码,直到现在,它已经非常基本了,但是没有太多可看的:

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ColumnSeries lC = new ColumnSeries();
            List<KeyValuePair<string, int>> valueList1 = new List<KeyValuePair<string, int>>();
            valueList1.Add(new KeyValuePair<string, int>("Developer", 60));
            valueList1.Add(new KeyValuePair<string, int>("Misc", 20));
            valueList1.Add(new KeyValuePair<string, int>("Tester", 50));
            valueList1.Add(new KeyValuePair<string, int>("QA", 30));
            valueList1.Add(new KeyValuePair<string, int>("Project Manager", 40));

            lC.DependentValuePath = "Value";
            lC.IndependentValuePath = "Key";
            lC.ItemsSource = valueList1;

            chart.Series.Add(lC);
        }
    }
}

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        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"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="grid">
        <chartingToolkit:Chart x:Name="chart">

        </chartingToolkit:Chart>
    </Grid>
</Window>

我在这里发现了这篇文章:WPF Column Chart Styling: Remove the gradient effect, set the color of the hash marks on y axis (the minor grid lines)。海报的第一个问题与我的问题几乎相同。第一个答案告诉他增加边框大小以填充列,但在我的程序中我仍然需要列边框,以便不是我想要的,这是唯一的答案所以它看起来像这样是一个非常棘手的问题。我将衷心感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

请学习如何设置WPF控件的样式,最简单的方法是使用Expression Blend。查看here for an example how to style图表控件

答案 1 :(得分:0)

要更改要在ColumnSeries.DataPointStyle中更改模板的外观。从后面的代码执行此操作会觉得非常不自然,所以我更改了您的代码只是为了能够 在XAML中定义样式。

<Window x:Class="WpfApplication1.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid x:Name="grid">
        <Grid.Resources>
            <!--Set ColumnColor-->
            <Brush x:Key="MyColumnColor">Red</Brush>
            <!--Style override-->
            <Style x:Key="MyColumnDataPointStyle"
                   TargetType="{x:Type chartingToolkit:ColumnDataPoint}">
                <Setter Property="Background"
                    Value="{StaticResource MyColumnColor}" />
                <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}">
                                    <!--Change Border appearance-->
                                    <Border BorderBrush="#CCFFFFFF"
                                        BorderThickness="1">
                                        <Border BorderBrush="#77FFFFFF"
                                        BorderThickness="1" />
                                    </Border>
                                </Grid>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <chartingToolkit:Chart>
            <chartingToolkit:ColumnSeries ItemsSource="{Binding ValueList}"
                                          DependentValuePath = "Value"
                                          IndependentValuePath = "Key"
                                          DataPointStyle="{StaticResource MyColumnDataPointStyle}">
            </chartingToolkit:ColumnSeries>
        </chartingToolkit:Chart>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls.DataVisualization.Charting;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainWindowViewModel
    {
        private List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>()
        {
            (new KeyValuePair<string, int>("Developer", 60)),
            (new KeyValuePair<string, int>("Misc", 20)),
            (new KeyValuePair<string, int>("Tester", 50)),
            (new KeyValuePair<string, int>("QA", 30)),
            (new KeyValuePair<string, int>("Project Manager", 40))
        };

        public List<KeyValuePair<string, int>> ValueList
        {
            get { return valueList; }
        }
    }
}