Вопрос

I got a FrameworkElement within the eventhandler of a class. Is there a possibility to check if this FrameworkElement is an element that has a borderthickness/borderbrush property ?

Like

var element = myframeworkelement as IHasBorder;
element.borderthickness = new Thickness(1,2,3,4);

Does something like this exist ?

Это было полезно?

Решение

The properties BorderThickness and BorderBrush are defined in the Control class.

So you can try to cast your FrameworkElement to Control, and if that works, set the properties:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        var pnl = new StackPanel();
        this.Content = pnl;

        var button = new Button();
        button.Content = "Hi";

        pnl.Children.Add(button);

        SetBorder(button);
    }

    public void SetBorder(FrameworkElement fe)
    {
        var borderControl = fe as Control;

        if (borderControl != null)
        {
            borderControl.BorderThickness = new Thickness(10);
            borderControl.BorderBrush = Brushes.Red;
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top