2011-01-20 15 views
0

私は、DrawingContextで矩形を描画するインフラストラクチャコードを持っています。その矩形にclickイベントが必要です。どうやってやるの ?DrawingContextで選択を実装する

これは私が長方形

dc.DrawRectangle(BG、脳卒中、RECT)を描画する方法です。

答えて

1

この矩形はピクセルであり、イベントを持つことはできません。

そのDCの所有者(Control)を確認する必要があります。または、Rectangle要素を使用してください。

0

VisualTreeHelperHitTest機能を使用できます。このような何かが(あなたがヒットをチェックしたいどこかでマウスをクリックしたときに、このコードを実行する)お手伝いをする必要があります。

HitTestResult result = VisualTreeHelper.HitTest(this, mouselocation); 

if (result.VisualHit.GetType() == typeof(DrawingVisual)) 
{ 
    //do your magic here. result.VisualHit will contain the rectangle that got hit 
} 
+0

Rectがヒットしません。それは視覚的ではありません。 –

0

あなたはItemsControlにを置き、長方形のコレクションにそのItemsSourceプロパティをバインドする必要があります。 次に、ItemsControl.ItemTemplateをオーバーライドし、Rectangleを表示するカスタムユーザーコントロールを含む独自の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> 

とRectangleView:メインウィンドウ

背後

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

そしてRectangleView背後にあるコード:

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

私はあなたがItemsControlに、ItemContainers、DataTemplatesとスタイルについての詳細を読むことをお勧めします。

P.S.時間制限のため、私はViewViewとModelLogicを、MainViewとRectangleViewの両方のクラスにまとめました。適切な実装では、MainModelがIEnumerable型のプロパティを宣言するだけでなく、MainView(MainModel)のプレゼンテーションモデルとRectangleView(RectangleViewData)のプレゼンテーションモデルを持つ必要があります。 RectangleViewDataはプロパティの変更を認識しないので、MainModelはそれを制御できます。

関連する問題