Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Selecting objects on a WPF canvas?
Message
De
26/02/2009 03:19:11
 
 
À
25/02/2009 23:22:44
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01383557
Message ID:
01384245
Vues:
32
Hi,
If you want the style to apply to all HolePattern objects (and only HolePattern objects or sub-classes) you can achieve this just in XAML. Make 'HolePattern' the TargetType and omit the x:Key attribute. The style will then be applied to all HolePattern objects regardless of how or when they are added to the Canvas.
HTH,
Viv

>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