2014-07-12 84 views
1

我正在用不同的項目編寫解決方案。有2個wpf客戶端,我使用caliburn micro編寫。他們都有一個地圖控件,它可以繪製一些圖層和一個列表視圖。我想在不同的項目中託管通用控件,以便這兩個應用程序都可以使用它們。這裏是我的代碼:添加自定義控件mvvm caliburn

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

我想通了,我可以添加構造

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}"/> 
相關問題