DVC Toolkit在WPF应用程序中的多行系列

时间:2019-01-28 08:45:36

标签: c# wpf visual-studio xaml wpf-controls

我试图在wpf应用程序中使用文本框来绘制多个线系列,这是代码和输出屏幕截图

Xaml代码

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:local="clr-namespace:EECc_Graph"
    xmlns:Compatible="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Compatible;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="EECc_Graph.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid x:Name="city2">
    <TextBox x:Name="t1" HorizontalAlignment="Left" Height="23" Margin="164,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="t2" HorizontalAlignment="Left" Height="23" Margin="164,178,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Label Content="city" HorizontalAlignment="Left" Margin="47,87,0,0" VerticalAlignment="Top" Width="66"/>
    <Label Content="temperature" HorizontalAlignment="Left" Margin="47,178,0,0" VerticalAlignment="Top" Width="94"/>


    <Button Content="Button" HorizontalAlignment="Left" Margin="115,311,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <Label Content="city2" HorizontalAlignment="Left" Margin="65,234,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="t3" HorizontalAlignment="Left" Height="23" Margin="164,234,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Label Content="temperature" HorizontalAlignment="Left" Margin="65,265,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="t4" HorizontalAlignment="Left" Height="23" Margin="190,265,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mchart"  Width="400" Height="250"  Loaded ="McChart_Loaded" Margin="339,107,53,62">
        <DVC:Chart.Series>
            <DVC:LineSeries Title=" weather"  IndependentValuePath="Key" DependentValuePath="Value" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" IsSelectionEnabled="True">

            </DVC:LineSeries>

            <DVC:LineSeries Title=" weathers"  IndependentValuePath="Key" DependentValuePath="Value"  ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" IsSelectionEnabled="True">

              </DVC:LineSeries>

        </DVC:Chart.Series>

    </DVC:Chart>

</Grid>
</Window>

c#代码

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

  using System.Windows.Controls.DataVisualization;
  using System.Windows.Controls.Primitives;
  using System.Windows.Controls.DataVisualization.Charting;
  using System.Collections.ObjectModel;

  namespace EECc_Graph
  {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollection<KeyValuePair<string, int>> ChartValues = new ObservableCollection<KeyValuePair<string, int>>();
    public ObservableCollection<KeyValuePair<string, int>> ChartValues1 = new ObservableCollection<KeyValuePair<string, int>>();


    public MainWindow()
    {
        InitializeComponent();
       this.mchart.DataContext = ChartValues;
        this.mchart.DataContext = ChartValues1;

        private void McChart_Loaded(object sender, RoutedEventArgs e)
    {

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
       string inputX = this.t1.Text.ToString();
        int inputY = Convert.ToInt32(this.t2.Text.ToString());
        ChartValues.Add(new KeyValuePair<string, int>(inputX, inputY));
        string inputX1 = this.t3.Text.ToString();
        int inputY1 = Convert.ToInt32(this.t4.Text.ToString());
        ChartValues1.Add(new KeyValuePair<string, int>(inputX1, inputY1));


    }

}
}

我可以知道我无法绘制多个线系列,集合1覆盖了集合2的数据上下文。绑定是否有任何错误。...enter image description here

这是我的输出屏幕截图,请帮忙

1 个答案:

答案 0 :(得分:1)

在后面的代码中,请添加以下代码:

public ObservableCollection<KeyValuePair<string, int>> ChartValues1 = new ObservableCollection<KeyValuePair<string, int>> ();
    public ObservableCollection<KeyValuePair<string, int>> ChartValues2 = new ObservableCollection<KeyValuePair<string, int>> ();

    public MainWindow () {
        InitializeComponent ();
        (line1 as LineSeries).ItemsSource = ChartValues1;
        (line2 as LineSeries).ItemsSource = ChartValues2;
    }

    private void Button_Click (object sender, RoutedEventArgs e) {
        //for line1
        string inputX = this.t1.Text.ToString ();
        int inputY = Convert.ToInt32 (this.t2.Text.ToString ());
        ChartValues1.Add (new KeyValuePair<string, int> (inputX, inputY));
        ChartValues1.Add (new KeyValuePair<string, int> ("c1 L-1", 22));
        ChartValues1.Add (new KeyValuePair<string, int> ("c2 L-1", 5));
        ChartValues1.Add (new KeyValuePair<string, int> ("c3 L-1", 19));

        //for line2
        string inputX1 = this.t3.Text.ToString ();
        int inputY1 = Convert.ToInt32 (this.t2.Text.ToString ());
        ChartValues2.Add (new KeyValuePair<string, int> (inputX1, inputY1));
        ChartValues2.Add (new KeyValuePair<string, int> ("c4 L-2", 17));
        ChartValues2.Add (new KeyValuePair<string, int> ("c5 L-2", 2));
        ChartValues2.Add (new KeyValuePair<string, int> ("c6 L-2", 21));
    }

回到xaml, 请使用以下代码段替换您的DVC。需要注意的是,这两个系列都添加了一个name属性,并在后面的代码中用于设置itemsource。

<DVC:Chart Canvas.Top="80" Canvas.Left="10"
               Name="mchart"
               Width="400" Height="250"
               Margin="339,107,53,62">
        <DVC:Chart.Series>
            <DVC:LineSeries Title=" weather" Name="line1"
                            IndependentValuePath="Key"
                            DependentValuePath="Value"
                            ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
                            IsSelectionEnabled="True">
            </DVC:LineSeries>
            <DVC:LineSeries Title=" weathers"  Name="line2"
                            IndependentValuePath="Key" DependentValuePath="Value"
                            ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
                            IsSelectionEnabled="True">
            </DVC:LineSeries>
        </DVC:Chart.Series>
    </DVC:Chart>

学习愉快!