我想用WPF开发一种矢量图像编辑器。 具有相同形式的每个形状都放在ItemsControl中。 至少有4种形状(线条,矩形,位图等)。 问题是:
我无法从另一层下面的图层中单击形状。
要求:
问题:
编辑1: 添加代码段
<Grid>
<!--Array of Ellipses-->
<ItemsControl ItemsSource="{Binding EllipseSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=Distance}" />
<Setter Property="Canvas.Width" Value="10" />
<Setter Property="Canvas.Top" Value="{Binding Path=Top}" />
<Setter Property="Canvas.Height" Value="10" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<Ellipse MouseDown="EllipseOnMouseDown" />
</ItemsControl.ItemTemplate>
</ItemsControl>
<!--Array of Rectangles-->
<ItemsControl ItemsSource="{Binding RectangleSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=Distance}" />
<Setter Property="Canvas.Width" Value="10" />
<Setter Property="Canvas.Top" Value="{Binding Path=Top}" />
<Setter Property="Canvas.Height" Value="10" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<Rectangle MouseDown="RectangleOnMouseDown" />
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
答案 0 :(得分:0)
注意:
grid
RectangleOnMouseDown
和EllipseOnMouseDown
更改为shape_MouseLeftButtonDown
您可以使用: XAML:
<Grid name="grid">
<!--Array of Ellipses-->
<ItemsControl ItemsSource="{Binding EllipseSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=Distance}" />
<Setter Property="Canvas.Width" Value="10" />
<Setter Property="Canvas.Top" Value="{Binding Path=Top}" />
<Setter Property="Canvas.Height" Value="10" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<Ellipse MouseDown="shape_MouseLeftButtonDown" />
</ItemsControl.ItemTemplate>
</ItemsControl>
<!--Array of Rectangles-->
<ItemsControl ItemsSource="{Binding RectangleSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=Distance}" />
<Setter Property="Canvas.Width" Value="10" />
<Setter Property="Canvas.Top" Value="{Binding Path=Top}" />
<Setter Property="Canvas.Height" Value="10" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<Rectangle MouseDown="shape_MouseLeftButtonDown" />
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
代码:
List<DependencyObject> hitResultsList = new List<DependencyObject>();
// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
hitResultsList.Add(result.VisualHit);
return HitTestResultBehavior.Continue;
}
private void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point pt = Mouse.GetPosition(grid);
hitResultsList.Clear();
VisualTreeHelper.HitTest(grid, null, new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(pt));
foreach (var ee in hitResultsList)
{
if(ee is Ellipse)
{
MessageBox.Show("rectangle clicked!");
var ellipse = ee as Ellipse;
// Do something with ellipse
}
if(ee is Rectangle)
{
MessageBox.Show("ellipse clicked!");
var rec = ee as Rectangle;
// Do something with rectangle
}
}
}