我有一个应用程序可以在屏幕上将插槽绘制为带圆角的矩形。一个槽可以是一个部分的一部分,它只不过是一个槽的集合。
我想直观地标记已分配的插槽。所以我想绘制一个覆盖所有选定插槽的实心形状。
选择前:
选择后:
形状必须没有内部边界,因此是组合几何。
我真的很想将 CombinedGeometry 绑定到所选插槽的数据模板。
视图模型
public ObservableCollection<Slot> Slots
{
get { return _slots; }
set
{
if (Equals(value, _slots)) return;
_slots = value;
DrawShape();
OnPropertyChanged();
}
}
private void DrawShape()
{
Path = new Path();
Path.Stroke = Brushes.White;
Path.StrokeThickness = 3;
var count = Slots.Count();
var combined = new CombinedGeometry();
combined.GeometryCombineMode = GeometryCombineMode.Union;
for (var i = 0; i < count; i++)
{
if (i == 0)
{
var rect1 = new Rect(Slots[i].PositionX, Slots[i].PositionY, Slots[i].Width, Slots[i].Height);
var rect2 = new Rect(Slots[i + 1].PositionX, Slots[i + 1].PositionY, Slots[i + 1].Width,
Slots[i + 1].Height);
combined.Geometry1 = new RectangleGeometry(rect1) { RadiusX = 5, RadiusY = 5 };
combined.Geometry2 = new RectangleGeometry(rect2) { RadiusX = 5, RadiusY = 5 };
}
else
{
var rect = new Rect(Slots[i + 1].PositionX, Slots[i + 1].PositionY, Slots[i + 1].Width, Slots[i + 1].Height);
combined.Geometry1 = combined.Clone();
combined.Geometry2 = new RectangleGeometry(rect) { RadiusX = 5, RadiusY = 5 };
}
}
Path.Data = combined;
}
XAML
<ItemsControl x:Name="DeclaredSections" Margin="20" ItemsSource="{Binding Path=Layout.Sections}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<!-- DATABIND MY COMBINED SHAPE -->
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>