开发者

WPF : Adding Border to an image programmatically

开发者 https://www.devze.com 2022-12-20 23:09 出处:网络
I want to add a style to the the image programmatically. Here is my code <UserControl.Resources>

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>
                        <Bor开发者_运维问答der 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


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; 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消