效果:
<Window x:Class="切换显示曲线.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:tk="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" Loaded="Window_Loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">选择Series1列:</TextBlock>
<ComboBox x:Name="cmbLine" SelectionChanged="cmbLine_SelectionChanged" SelectedValuePath="Name" Grid.Column="1"></ComboBox>
<TextBlock Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center">选择Series2列:</TextBlock>
<ComboBox x:Name="cmbLine1" SelectionChanged="cmbLin1_SelectionChanged" SelectedValuePath="Name" Grid.Column="3"></ComboBox>
<tk:Chart Name="chart1" Grid.Row="1" Grid.ColumnSpan="4" >
<tk:Chart.Axes>
<tk:LinearAxis Orientation="X" Interval="1"></tk:LinearAxis>
</tk:Chart.Axes>
<tk:LineSeries x:Name="line1" IndependentValuePath="X" DependentValuePath="Y1" ItemsSource="{Binding}">
<tk:LineSeries.DependentRangeAxis>
<tk:LinearAxis Orientation="Y" Title="{Binding SelectedValue,ElementName=cmbLine}" Interval="1" ></tk:LinearAxis>
</tk:LineSeries.DependentRangeAxis>
</tk:LineSeries>
<tk:LineSeries x:Name="line2" IndependentValuePath="X" DependentValuePath="Y1" ItemsSource="{Binding}" >
<tk:LineSeries.DependentRangeAxis>
<tk:LinearAxis Orientation="Y" Title="{Binding SelectedValue,ElementName=cmbLine1}" Interval="1"></tk:LinearAxis>
</tk:LineSeries.DependentRangeAxis>
</tk:LineSeries>
</tk:Chart>
</Grid>
</Window>
读取一个类的所有属性:
PropertyInfo[] strs = new XYS().GetType().GetProperties();
常用属性
PropertyInfo prop = strs[0];
string str = prop.Name;//属性名
bool bl = prop.CanRead;//是否可读
bool bl1 = prop.CanWrite;//是否可写
string str1 = prop.PropertyType.Name;//数据类型
bool bl2 = prop.PropertyType.IsArray;//是否为数组
bool bl3 = prop.PropertyType.IsEnum;//是否是枚举
后台代码:
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.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Controls.DataVisualization.Charting;namespace 切换显示曲线
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<XYS> list = new List<XYS>();
list.Add(new XYS(1, 1, 3, 5, 7));
list.Add(new XYS(2, 2, 4, 6, 8));
list.Add(new XYS(3, 3, 6, 9, 12));
list.Add(new XYS(4, 4, 8, 12,16));this.DataContext = list;
PropertyInfo[] strs = new XYS().GetType().GetProperties();
//foreach (PropertyInfo pi in strs)
//{
// MessageBox.Show(pi.Name);
//}cmbLine.ItemsSource = strs;
cmbLine.DisplayMemberPath = "Name";
//cmbLine.SelectedIndex = 1;cmbLine1.ItemsSource = strs;
cmbLine1.DisplayMemberPath = "Name";
//cmbLin1.SelectedIndex = 2;
}private void cmbLine_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PropertyInfo pi = cmbLine.SelectedItem as PropertyInfo;
line1.DependentValuePath = pi.Name;
//LinearAxis la = new LinearAxis();
//la.Orientation = AxisOrientation.Y;
//la.Title = pi.Name;
//line1.DependentRangeAxis = la;
}private void cmbLin1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PropertyInfo pi = cmbLine1.SelectedItem as PropertyInfo;
line2.DependentValuePath = pi.Name;//LinearAxis la = new LinearAxis();
//la.Orientation = AxisOrientation.Y;
//la.Title = pi.Name;
//line2.DependentRangeAxis = la;}
}class XYS
{
public int X { get; set; }
public int Y1 { get; set; }
public int Y2 { get; set; }
public int Y3 { get; set; }
public int Y4 { get; set; }public XYS(int x,int y1,int y2,int y3,int y4)
{
X = x;
Y1 = y1;
Y2 = y2;
Y3 = y3;
Y4 = y4;
}public XYS()
{
;
}
}
}
WPF Toolkit Chart--动态换列