添加自定义控件mvvm校准

本文关键字:校准 mvvm 自定义控件 添加 | 更新日期: 2023-09-27 18:12:41

我正在写一个不同项目的解决方案。有2个wpf客户端是我用caliburn micro编写的。它们都有一个绘制图层的地图控件和一个列表视图。我希望在不同的项目中托管通用控件,以便两个应用程序都可以使用它们。下面是我的代码:

    namespace Core.WPF.ViewModels
{
    public class FieldMapControlViewModel : Caliburn.Micro.PropertyChangedBase
    {
        public ObservableCollection<Field> Fields
        {
            get { return new ObservableCollection<Field>(FieldPolys.Select(x => x.Field)); }
        }
        private ObservableCollection<Brush> _brushes;
        public ObservableCollection<Brush> Brushes
        {
            get { return _brushes; }
            set
            {
                _brushes = value;
                NotifyOfPropertyChange(() => Brushes);
                UpdatePolyFill();
                NotifyOfPropertyChange(() => InfoLayer);
            }
        } 

        private ObservableCollection<FieldPoly> _fieldPolys;
        public ObservableCollection<FieldPoly> FieldPolys
        {
            get { return _fieldPolys; }
            set
            {
                _fieldPolys = value;
                NotifyOfPropertyChange(() => FieldPolys);
                NotifyOfPropertyChange(() => Fields);
                NotifyOfPropertyChange(() => InfoLayer);
            }
        }
        private FieldPoly _selectedFieldPoly;
        public FieldPoly SelectedFieldPoly
        {
            get { return _selectedFieldPoly; }
            set
            {
                _selectedFieldPoly = value;
                NotifyOfPropertyChange(() => SelectedFieldPoly);
                NotifyOfPropertyChange(() => SelectedField);
                UpdatePolyFill();
            }
        }
        public Field SelectedField
        {
            get
            {
                if (SelectedFieldPoly != null) return SelectedFieldPoly.Field;
                return null;
            }
            set
            {
                SelectedFieldPoly = FieldPolys.Where(x => x.Field == value).FirstOrDefault();
                NotifyOfPropertyChange(() => SelectedField);
            }
        }
        public ObservableCollection<Control> InfoLayer
        {
            get
            {
                if (FieldPolys.Count() > 0)
                {
                    return new ObservableCollection<Control>(FieldPolys.Select(x => x.Poly));
                }
                return new ObservableCollection<Control>();
            }
        }
        public void UpdatePolyFill()
        {
            foreach (var fp in FieldPolys)
            {
                fp.Poly.GetBindingExpression(MapPolygon.FillProperty).UpdateTarget();
            }
        }
    }
    public class FieldPoly
    {
        public Field Field
        {
            get;
            private set;
        }
        public bool IsSelected
        {
            get { return shell.SelectedFieldPoly == this; }
        }
        FieldMapControlViewModel shell;
        private MapPolygon _poly;
        public MapPolygon Poly
        {
            get { return _poly; }
            private set
            {
                _poly = value;
            }
        }
        public FieldPoly(FieldMapControlViewModel shell, Field field, Brush brush)
        {
            this.shell = shell;
            Field = field;
            Poly = new MapPolygon();
            Poly.Points = new LocationCollection();
            foreach (var point in field.FieldPoints)
            {
                Poly.Points.Add(new Location(point.Y, point.X));
            }
            Binding bind = new Binding("Brush");
            bind.Source = this;
            Poly.SetBinding(MapPolygon.FillProperty, bind);
            Poly.MouseLeftButtonUp += poly_MouseLeftButtonUp;
        }
        private Brush _brush;
        public Brush Brush
        {
            get
            {
                return _brush;
            }
            set
            {
                _brush = value;
            }
        }

    void poly_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        shell.SelectedFieldPoly = this;
    }
}

}

如何在应用程序视图中添加:

<Views:FieldMapControlView  FieldPolys={Binding FieldPolys} />

添加自定义控件mvvm校准

我发现我可以添加构造函数

public FieldMapControlViewModel(IEnumerable<Field> Fields)
            {
                FieldPolys = new ObservableCollection<FieldPoly>();
                foreach (var f in Fields)
                {
                    FieldPolys.Add(new FieldPoly(this, f,System.Windows.Media.Brushes.Red));
                }
            }

,然后在应用程序的xaml中使用

<Views:FieldMapControlView x:Name="FieldMap" cal:Bind.Model="{Binding FieldMap}"/>