简体   繁体   中英

C# WPF Add border to Image on Click event

I have a C# WPF application. In this application I got a canvas to which I programmatically add images ( Canvas.Children.Add(MyImage) ). (I also have some MouseMove events on the individual images.)

My question is: How can I add an event to the images so I know when they are selected, and also add a border to the currently selected Image.

I've done something similar before with a TextBox:

<Canvas.Style>
    <Style TargetType="{x:Type Image}"><!-- With TextBox instead of Image, this works -->
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="true">
                <Setter Property="BorderBrush" Value="Red"/>
                <Setter Property="BorderThickness" Value="2"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Canvas.Style>

But because Image doesn't have a BorderBrush / BorderThickness property, the method above doesn't work. Is there any way to add a border to a selected Image? And also to remove the border again when another Image is selected?

Thanks in advance for the responses.

You need to use the Border control .

  1. Change the TargetType to Border in your XAML.
  2. In your code behind add your image to the Child property of a new Border control.
  3. Add the border to the Canvas.Children

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM