WPF重绘所有子UI

时间:2015-03-18 13:06:26

标签: c# wpf

我所需要的只是在线显示工具提示。 在什么时候我应该在控件上添加孩子?需要在绑定模型后添加它们。

我尝试在OnRender中执行此操作:

    protected override void OnRender(DrawingContext context)
    {
        base.OnRender(context);

        _planetsPos = new Dictionary<int, Point>();
        Pen pen = new Pen((SolidColorBrush)(new BrushConverter().ConvertFrom("#000000")), 1.0);
        context.DrawEllipse(Brushes.White, pen, new Point(CentreX, CentreY), Radius, Radius);
        pen = new Pen((SolidColorBrush)(new BrushConverter().ConvertFrom("#00C800")), 1.0);
        if (!_load && Planets.Count > 0)
        {
            _aspects = new Dictionary<PlanetModel, List<AspectUI>>();
            for (int i = 0; i < Planets.Count; i++)
            {
                List<AspectUI> list = new List<AspectUI>();
                for (int j = 0; j < Planets.Count; j++)
                {
                    var aspect = new AspectUI(Planets[j]);
                    list.Add(aspect);
                    if (i != j)
                    {
                        this.Children.Add(aspect);
                    }
                }
                _aspects.Add(Planets[i], list);
            }
            _load = true;
        }

        for (int i = 0; i < Planets.Count; i++)
        {
            for (int j = 1; j < Planets.Count; j++)
            {
                if (i != j)
                {
                    var delta = Math.Abs(Planets[i].Longitude - Planets[j].Longitude);
                    for (int k = 0; k < Orbs.Orbs.Count; k++)
                    {
                        var orb = Orbs.Orbs[k];
                        pen = new Pen((SolidColorBrush)(new BrushConverter().ConvertFrom("#" + orb.Color)), 1.0);
                        var obj = _aspects.Where(w => w.Key.Code == Planets[i].Code);
                        if (null != obj)
                        {
                            var aspectUI = obj.FirstOrDefault().Value[j];
                            int ind = this.Children.IndexOf(aspectUI);
                            if (ind > -1)
                            {
                                var child = (AspectUI)this.Children[ind];
                                SetAspectUIData(child, pen, i, j, delta, orb);
                            }
                        }
                    }
                }
            }
        }
    }

private void SetAspectUIData(AspectUI aspectUI, Pen pen, int i, int j, double delta, Orb orb)
        {
            double orbValue = (double)GetPropValue(orb, "Pl" + i.ToString());
            if ((orb.Aspect <= delta && orb.Aspect + orbValue >= delta) || 
                (orb.Aspect >= delta && orb.Aspect - orbValue <= delta))
            {
                aspectUI.SetAspectData(_planetsPos[Planets[i].Code], _planetsPos[Planets[j].Code], pen,
                    String.Format("{0} {1} - {2}", orb.Name, Planets[i].Name, Planets[j].Name));
                aspectUI.Visibility = Visibility.Visible;
            }
            else
            {
                aspectUI.Visibility = Visibility.Hidden;
            }
        }

如何更新这些控件?未调用AspectUI的OnRender。

using ISweEph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Zodiac.Controls
{
    public class AspectUI : Canvas
    {
        PlanetModel _planet;
        Point _startPoint;
        Point _endPoint;
        Pen _pen;
        public AspectUI(PlanetModel planet)
        {
            _planet = planet;
        }

        public void SetAspectData(Point startPoint, Point endPoint, Pen pen, String toolTip)
        {
            _startPoint = startPoint;
            _endPoint = endPoint;
            _pen = pen;
            ToolTip = toolTip;
        }

        protected override void OnRender(DrawingContext context)
        {
            base.OnRender(context);

            context.DrawLine(_pen, _startPoint, _endPoint);
        }
    }
}

0 个答案:

没有答案