数据源增加SeriesSource

使用方式

<Charting:Chart x:Name="chart"
Helper:ChartHelper.DependentValueBinding="Value"
Helper:ChartHelper.IndependentValueBinding="Key"
Helper:ChartHelper.Title="TitlePropertyOnCollection"
Helper:ChartHelper.SeriesType="Line"
Helper:ChartHelper.SeriesSource="{Binding Path=MyCollectionofCollections}" />
 

增加依赖属性

public enum SeriesType
{
Line,
Bar,
Column,
Scatter,
Pie
}
public class ChartHelper
{
#region SeriesSource public static readonly DependencyProperty SeriesSourceProperty =
DependencyProperty.RegisterAttached("SeriesSource",
typeof(IEnumerable),
typeof(ChartHelper),
new PropertyMetadata(SeriesSourceChanged)); public static IEnumerable GetSeriesSource(DependencyObject d)
{
return (IEnumerable)d.GetValue(SeriesSourceProperty);
} public static void SetSeriesSource(DependencyObject d, IEnumerable value)
{
d.SetValue(SeriesSourceProperty, value);
} #endregion #region DependentValueBinding public static readonly DependencyProperty DependentValueBindingProperty =
DependencyProperty.RegisterAttached("DependentValueBinding",
typeof(string),
typeof(ChartHelper),
null); public static string GetDependentValueBinding(DependencyObject d)
{
return (string)d.GetValue(DependentValueBindingProperty);
} public static void SetDependentValueBinding(DependencyObject d, string value)
{
d.SetValue(DependentValueBindingProperty, value);
} #endregion #region IndependentValueBinding public static readonly DependencyProperty IndependentValueBindingProperty =
DependencyProperty.RegisterAttached("IndependentValueBinding",
typeof(string),
typeof(ChartHelper),
null); public static string GetIndependentValueBinding(DependencyObject d)
{
return (string)d.GetValue(IndependentValueBindingProperty);
} public static void SetIndependentValueBinding(DependencyObject d, string value)
{
d.SetValue(IndependentValueBindingProperty, value);
} #endregion #region Title public static readonly DependencyProperty TitleProperty =
DependencyProperty.RegisterAttached("Title",
typeof(string),
typeof(ChartHelper),
null); public static string GetTitle(DependencyObject d)
{
return (string)d.GetValue(TitleProperty);
} public static void SetTitle(DependencyObject d, string value)
{
d.SetValue(TitleProperty, value);
} #endregion #region SeriesType public static readonly DependencyProperty SeriesTypeProperty =
DependencyProperty.RegisterAttached("SeriesType",
typeof(SeriesType),
typeof(ChartHelper),
new PropertyMetadata(SeriesType.Bar)); public static SeriesType GetSeriesType(DependencyObject d)
{
return (SeriesType)d.GetValue(SeriesTypeProperty);
} public static void SetSeriesType(DependencyObject d, SeriesType value)
{
d.SetValue(SeriesTypeProperty, value);
} #endregion #region SeriesStyle public static readonly DependencyProperty SeriesStyleProperty =
DependencyProperty.RegisterAttached("SeriesStyle",
typeof(Style),
typeof(ChartHelper),
null); public static Style GetSeriesStyle(DependencyObject d)
{
return (Style)d.GetValue(SeriesStyleProperty);
} public static void SetSeriesStyle(DependencyObject d, Style value)
{
d.SetValue(SeriesStyleProperty, value);
} #endregion private static void SeriesSourceChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if(!(d is Chart))
{
throw new Exception("Series attached property only works on a Chart type");
} var chart = d as Chart; /* Clear out any old series in the chart */
chart.Series.Clear(); /* Get our collection of data we need for each series */
var chartSeriesSource = e.NewValue as IEnumerable; if(chartSeriesSource == null)
throw new Exception("The SeriesSource does not support IEnumerable"); /* Loop over each collection of data */
foreach(var dataSource in chartSeriesSource)
{
DynamicSeries series; /* Find out what type of series we want to use */
var seriesType = GetSeriesType(chart); switch(seriesType)
{
case SeriesType.Line:
series = new LineSeries();
break;
case SeriesType.Bar:
series = new BarSeries();
break;
case SeriesType.Column:
series = new ColumnSeries();
break;
case SeriesType.Pie:
series = new PieSeries();
break;
case SeriesType.Scatter:
series = new ScatterSeries();
break;
default:
throw new ArgumentOutOfRangeException();
} /* Get and set the style of the newly created series */
var seriesStyle = GetSeriesStyle(chart);
series.Style = seriesStyle; string titleBindingName = GetTitle(chart); if (!string.IsNullOrEmpty(titleBindingName))
{
/* Do some binding of the Title property */
var titleBinding = new Binding(titleBindingName)
{
Source = series.Title, Mode = BindingMode.TwoWay
}; series.SetBinding(Series.TitleProperty, titleBinding);
} /* Setup the bindings configured in the attached properties */
series.DependentValueBinding = new Binding(GetDependentValueBinding(chart));
series.IndependentValueBinding = new Binding(GetIndependentValueBinding(chart)); /*Set the ItemsSource property, which gives the data to the series to be rendered */
series.ItemsSource = dataSource as IEnumerable; /* Add the series to the chart */
chart.Series.Add(series);
}
}
}

最新文章

  1. Java程序设计之整数分解
  2. 170105、MySQL 性能优化的最佳 20+ 条经验
  3. 从输入 URL 到浏览器接收的过程中发生了什么事情
  4. php操作mysql数据库
  5. 深入理解Java:注解(Annotation)基本概念
  6. add user
  7. Storm系列之二——Guaranteeing Message Processing
  8. python 练习 2
  9. STL 速解
  10. JavaScript高级程序设计43.pdf
  11. 【转】C++读写二进制文件
  12. 关于code reivew
  13. Python笔记4-20151029
  14. 从零开始理解JAVA事件处理机制(3)
  15. HTML (Hyper Text Markup Language) 常用标签
  16. JavaScript定时器:setTimeout()和setInterval()
  17. SmartUpload文件上传组件的使用教程
  18. SSM框架整合搭建教程
  19. Entity Framework学习 - 5.DB First执行时提示model没有key
  20. 使用外网访问阿里云服务器ZooKeeper

热门文章

  1. [Asp.net 5] Localization-resx资源文件的管理
  2. WebApi设置SessionState为Required
  3. IM聊天系统
  4. InteliJ Shortcuts
  5. SQL复制表结构或表数据
  6. table tr foreach td 换行
  7. HTML5移动端图片左右切换动画
  8. 在Autodesk应用程序商店发布基于浏览器的Web应用程序
  9. [转]Design Pattern Interview Questions - Part 3
  10. Objective-C 快速入门--基础(五)