Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Selecting objects on a WPF canvas?
Message
De
25/02/2009 23:22:44
 
 
À
25/02/2009 03:55:13
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01383557
Message ID:
01384225
Vues:
27
Wow, that's a cool trick.

Your example was adding the Ellipse and setting the style in XAML, but my Ellipses are added to the canvas in a method in code-behind.

So, you forced me to learn how to reference a StaticResource in code-behind. It took me about 15 minutes of digging around on the internet, and in Help, but I did figure it out. Thanks for that lesson.

Now, I've added the style you gave me to my window resources, and then I set the style as they are added in code-behind:
        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);
                    Hole.HoleEntity.Style = (Style)this.FindResource("EllipseStyle");   <---- Had to learn this part!!!
                    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