我正在编写一个包含不同项目的解决方案。我使用caliburn micro编写了2个wpf客户端。它们都有一个地图控件,可以绘制一些图层和列表视图。我想在不同的项目中托管常用控件,以便两个应用程序都可以使用它们。这是我的代码:
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} />
答案 0 :(得分:0)
我发现我可以添加构造函数
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}"/>