本文作者:qiaoqingyi

WPF专业编程指南(wpf 网络编程)

qiaoqingyi 01-09 106

  本文主要介绍如何在C1Chart for WPF中创建一个可以移动的线,使得当鼠标拖拽的时候,可以在Chart上自由移动。为了显示拖拽的线,需要创建一个Marker。本文创建一条竖直的Marker,通过创建ChartPanelObject实现,将ChartPanelObject的Action属性设置为LeftButtonDrag,实现鼠标左键的拖拽。

  使用ChartPanelObject创建竖直的线,代码参考:

  ChartPanelObject CreateMarker(bool isHorizontal) { var obj = new ChartPanelObject(); var bdr = new Border() { BorderBrush = Background = new SolidColorBrush(Color.FromArgb(150, 255, 2, 2)), Padding = new Thickness(2), }; var tb = new TextBlock(); var bind = new Binding(); bind.Source = obj; if (isHorizontal) { bdr.BorderThickness = new Thickness(0, 4, 0, 0); bdr.Margin = new Thickness(0, -1, 0, 0); obj.HorizontalContentAlignment = HorizontalAlignment.Stretch; bind.StringFormat = "y={0:#.#}"; bind.Path = new PropertyPath("DataPoint.Y"); obj.DataPoint = new Point(double.NaN, 0.5); } else { bdr.BorderThickness = new Thickness(4, 0, 0, 0); bdr.Margin = new Thickness(-1, 0, 0, 0); obj.VerticalContentAlignment = VerticalAlignment.Stretch; bind.StringFormat = "x={0:#.#}"; bind.Path = new PropertyPath("DataPoint.X"); obj.DataPoint = new Point(2, double.NaN); } tb.SetBinding(TextBlock.TextProperty, bind); bdr.Child = tb; bdr.IsHitTestVisible = true; obj.Content = bdr; return obj; }

  通过bind.Path = new PropertyPath("DataPoint.X"),让这条拖拽的线展示X轴的数据。 实现鼠标左键的代码参考:

  var vmarker = CreateMarker(false); pnl.Children.Add(vmarker); vmarker.Action = ChartPanelAction.LeftMouseButtonDrag; chart.View.Layers.Add(pnl);

  实现的效果如下:

  

  PS: 关于ComponentOne,这些产品你可以关注本文转载自葡萄城新年新禧新气象,送礼送福送优惠!优惠详情点击查看

WPF专业编程指南(wpf 网络编程)

  有用(0)没用(0)

  本站文章除注明转载外,均为本站原创或翻译

  欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果

  转载请注明:文章转载自:慧都控件网 [https://www.evget.com]

  本文地址:https://www.evget.com/article/2016/1/12/23375.html

阅读
分享