문제

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