2016-07-27 112 views
0

我目前正在研究一個簡單的c#WPF程序,它必須顯示一個列圖表。然而,System.Windows.Controls.DataVisualization.Charting命名空間的基本柱形圖帶有梯度列填充顏色,我不想在程序中使用。WPF圖表刪除漸變填充

有沒有什麼辦法,無論是在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)這裏。海報的第一個問題與我的幾乎相同。第一個答案告訴他增加邊框大小來填充列,但在我的程序中,我仍然需要列邊框,所以這不是我想要的,這是唯一的答案,所以看起來這是一個非常棘手的問題。我將衷心感謝您的幫助。謝謝!

回答

0

要更改您要更改ColumnSeries.DataPointStyle中模板的外觀。要從後面的代碼執行此操作會感覺非常不自然,所以我更改了代碼,以便能夠在XAML中定義Style。

<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; } 
     } 
    } 
} 
0

請學習如何設計WPF控件,最簡單的方法是使用Expression Blend。查看here for an example how to style製圖控制

+0

感謝您的鏈接,正是我所需要的東西。一直在尋找一個圖表造型的好例子,但沒有找到太多...... – zockDoc