user279244
user279244

Reputation: 971

WPF : Adding Border to an image programmatically

I want to add a style to the the image programmatically. Here is my code

<UserControl.Resources>
       <Style x:Name="BranchPages" x:Key="BranchPages">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderThickness="2" BorderBrush="Green">
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
</UserControl.Resources>

and the code behid is as follows

  Style greenbdr = (Style)FindResource("BranchPages");
  page.img.Style = greenbdr;

But Its not working Please help

Upvotes: 5

Views: 12499

Answers (2)

Jojo Sardez
Jojo Sardez

Reputation: 8568

This workaround might help:

Since the Image has no border, place it inside a Border control.

<Border Name="imgBorder" BorderThickness="2" BorderBrush="Transparent">
        <Image Name="img"></Image>
</Border>

Then create logic code against the properties of that Border.

imgBorder.BorderBrush = Brushes.Green; 

Upvotes: 9

Timores
Timores

Reputation: 14589

An Image is not a Control, it is only derived from FrameworkElement and thus has no Template property. It has a Style, though, so you can use it to set its properties, like Cursor, HorizontalAlignment, etc.

Upvotes: 0

Related Questions