Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在DrawingContext中实现选择_C#_Wpf - Fatal编程技术网

C# 在DrawingContext中实现选择

C# 在DrawingContext中实现选择,c#,wpf,C#,Wpf,我有一个使用DrawingContext绘制矩形的基础结构代码,我希望这些矩形具有click事件。我该怎么做 这就是我画矩形的方法 dc.DrawRectangle(背景、笔划、矩形) 那个矩形只是像素,它不能有任何事件 您必须查看该DC的所有者(控制权)。或者只使用矩形元素 您可以使用VisualTreeHelper的HitTest功能。类似的内容应该会对您有所帮助(当您在想要检查点击的地方单击鼠标时,执行此代码): 您应该放置ItemsControl并将其ItemsSource属性绑定到矩形

我有一个使用DrawingContext绘制矩形的基础结构代码,我希望这些矩形具有click事件。我该怎么做

这就是我画矩形的方法


dc.DrawRectangle(背景、笔划、矩形)

那个矩形只是像素,它不能有任何事件


您必须查看该DC的所有者(控制权)。或者只使用
矩形
元素

您可以使用
VisualTreeHelper
HitTest
功能。类似的内容应该会对您有所帮助(当您在想要检查点击的地方单击鼠标时,执行此代码):


您应该放置ItemsControl并将其ItemsSource属性绑定到矩形集合。 然后您应该重写ItemsControl.ItemTemplate,并提供您自己的DataTemplate,其中包含显示矩形的自定义用户控件。此用户控件能够处理鼠标事件

主机窗口的XAML:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:self ="clr-namespace:Test"
    Title="MainWindow" 
    Height="350" Width="700">
<Window.Resources>
    <DataTemplate x:Key="RectTemplate">
        <self:RectangleView />
    </DataTemplate>
</Window.Resources>
<Grid>
    <ItemsControl ItemsSource="{Binding Rectangles}" 
                  ItemTemplate="{StaticResource RectTemplate}">
    </ItemsControl>
</Grid>
我建议您阅读更多关于ItemsControl、ItemContainers、DataTemplates和Styles的内容


另外,由于时间限制,我将视图和模型逻辑合并为一个类,用于MainView和RectangleView。在良好的实现中,除了MainModel声明IEnumerable类型的属性外,还应该有MainView的表示模型(MainModel)和RectangleView的表示模型(RectangleViewData)。由于矩形视图数据不涉及属性更改,MainModel可以控制它

Rect不会命中。这不是视觉上的。
<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:self ="clr-namespace:Test"
    Title="MainWindow" 
    Height="350" Width="700">
<Window.Resources>
    <DataTemplate x:Key="RectTemplate">
        <self:RectangleView />
    </DataTemplate>
</Window.Resources>
<Grid>
    <ItemsControl ItemsSource="{Binding Rectangles}" 
                  ItemTemplate="{StaticResource RectTemplate}">
    </ItemsControl>
</Grid>
<UserControl x:Class="Test.RectangleView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" >
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public IEnumerable<Rect> Rectangles
    {
        get 
        {
            yield return new Rect(new Point(10, 10), new Size(100, 100));
            yield return new Rect(new Point(50, 50), new Size(400, 100));
            yield return new Rect(new Point(660, 10), new Size(10, 100));
        }
    }
}
public partial class RectangleView : UserControl
{
    public RectangleView()
    {
        InitializeComponent();
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        drawingContext.DrawRectangle(Brushes.Orchid, new Pen(Brushes.OliveDrab, 2.0), (Rect)this.DataContext);
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        // HERE YOU CAN PROCCESS MOUSE CLICK
        base.OnMouseDown(e);
    }
}