Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Selecting objects on a WPF canvas?
Message
De
25/02/2009 03:55:13
 
 
À
23/02/2009 14:26:20
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01383557
Message ID:
01383986
Vues:
39
>I have a WPF Canvas with some Ellipse objects on it (displayed as circles). Each circle is from a collection class instance which is actually a custom hole pattern class. Each pattern has a certain number of circles, and each circle then gets added to the canvas using an iteration over the collection using the code below.
>
>So, the canvas is populated with a bunch of circles and each circle belongs to a certain pattern instance. You can see a screenshot here: http://twitpic.com/1f2ci/full
>
>Now I want to add the ability to click on a circle on the canvas, and be able to determine the collection it belongs to, so that I can then do some more work on the selected pattern to which that circle belongs.
>
>
>public void DrawHoles()   
>{   
>   // Iterate over each HolePattern in the HolePatterns collection...    
>   foreach (HolePattern HolePattern in HolePatterns)   
>    {   
>        // Now iterate over each Hole in the HoleList of the current HolePattern...   
>        foreach (Hole Hole in HolePattern.HoleList)   
>        {   
>            Hole.CanvasX = SketchX0 + (Hole.AbsX * _ZoomScale);   
>            Hole.CanvasY = SketchY0 - (Hole.AbsY * _ZoomScale);   
>            canvas1.Children.Add(Hole.HoleEntity);   
>        }   
>    }   
>}  
>
Going back to basics on this: another approach would be to add a style to your shape:
<Window x:Class="WpfApplication1.Window17"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window17" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="Ellipse" x:Key="xxx">
            <Setter Property="Stroke" Value="Black"/>
            <Setter Property="StrokeThickness" Value="1" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true" >
                    <Setter Property="Fill" Value="Yellow"/>
                    <Setter Property="StrokeThickness" Value="5" />
                    <Setter Property="Stroke" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
        <Canvas>
        <Ellipse Style="{StaticResource xxx}"  Canvas.Top="10" Canvas.Left="10" Width="40" Height="40" >
        </Ellipse>
    </Canvas>
</Window>
You still have to sneak up on it a bit but it will be obvious when the mousedown will target the item in question - and because you have a temporary fill anywhere within the bounds will count as a hit...
HTH,
Viv
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform