/// <summary> /// 渲染项目 /// </summary> private void RenderItems() { if (Data == null) { return; } for (int i = 0; i < Data.Count(); i++) { ChartDataModel data = Data.ElementAt(i); ChartItem item = GetCreateItem(data, MaxValue); if (i > 0) { //添加间距 item.Margin = new Thickness(GapValue, 0, 0, 0); } ItemContainer.Children.Add(item); } }
/// <summary> /// 容器滚动 /// </summary> /// <param name="d">0左,1右</param> private void Scroll(int d) { if (ItemsScrollViewer != null && ItemsScrollViewer.ComputedHorizontalScrollBarVisibility == Visibility.Visible && !isScrollAnimationActive ) { isScrollAnimationActive = IsAnimation; //取一个数据控件 double LostWidth = 0; if (ItemContainer.Children.Count > 1) { //取第二个,才有margin属性 ChartItem chartItem = ItemContainer.Children[1] as ChartItem; double oneItemWidth = chartItem.ActualWidth + chartItem.Margin.Left; //每屏最多多少个项目 double onePageMaxItemNum = ItemsScrollViewer.ActualWidth / oneItemWidth; if ((int)onePageMaxItemNum != onePageMaxItemNum) { //带小数点,需要计算 LostWidth = oneItemWidth - double.Parse(("0." + onePageMaxItemNum.ToString().Split('.')[1])) * oneItemWidth - chartItem.Margin.Left; } } //计算一屏滚动值 double onePageScrollValue = ItemsScrollViewer.ActualWidth - LostWidth - 2; //滚动值 double to; if (d == 0) { //左滚 double leftScrollValue = ItemsScrollViewer.HorizontalOffset - onePageScrollValue; to = leftScrollValue > 0 ? leftScrollValue : 0; } else { //右滚 double rightScrollValue = ItemsScrollViewer.HorizontalOffset + onePageScrollValue; to = rightScrollValue < ItemsScrollViewer.ScrollableWidth ? rightScrollValue : ItemsScrollViewer.ScrollableWidth; } if (ItemsScrollViewer.HorizontalOffset == to) { //不执行无变化的动画 isScrollAnimationActive = false; return; } if (IsAnimation) { OnAnimationLockEvent?.Invoke(this, true, 0); scrollAnimation.From = ItemsScrollViewer.HorizontalOffset; scrollAnimation.To = to; scrollStoryboard.Begin(); } else { ItemsScrollViewer.ScrollToHorizontalOffset(to); HandleScrollViewerState(); } } }