星空网 > 软件开发 > ASP.net

WPFTookit Chart 高级进阶

数据源增加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);    }  }}



原标题:WPFTookit Chart 高级进阶

关键词:wpf

wpf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

外媒消息:eBay控诉亚马逊非法招募其平台卖家!!!:https://www.ikjzd.com/articles/9061
电商市场退货情况报告:美国每年因退货欺诈损失约176亿美元!:https://www.ikjzd.com/articles/90611
收藏:老外推荐25个可大幅提高打开率的开发信标题!:https://www.ikjzd.com/articles/90615
竞争小、利润高:潜力品类MRO不容错过:https://www.ikjzd.com/articles/90616
惊呆!印尼本土电商一天50000单:https://www.ikjzd.com/articles/90617
推荐一款曝光率神器:eBay推广利器促销刊登:https://www.ikjzd.com/articles/90618
天坛最佳攻略 天坛必玩景点:https://www.vstour.cn/a/408240.html
央视新址为什么会找回:https://www.vstour.cn/a/408241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流