gpt4 book ai didi

c# - 添加自定义控件mvvm caliburn

转载 作者:行者123 更新时间:2023-12-03 10:46:16 27 4
gpt4 key购买 nike

我正在用不同的项目编写解决方案。我使用caliburn micro编写了2个wpf客户端。它们都具有一个 map 控件,该控件绘制了一些图层和一个listview。我想在不同项目中托管通用控件,以便两个应用程序都可以使用它们。这是我的代码:

    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;
}
}

}

我如何通过添加以下内容来在应用程序 View 中解决此控件问题:
<Views:FieldMapControlView  FieldPolys={Binding FieldPolys} />

最佳答案

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

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}"/>

关于c# - 添加自定义控件mvvm caliburn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24713539/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com