检查FrameworkElement是否有Border

时间:2012-11-11 09:40:11

标签: c# .net wpf frameworkelement

我在一个类的事件处理程序中得到了一个FrameworkElement。是否有可能检查此FrameworkElement是否是具有borderthickness / borderbrush属性的元素?

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

这样的事情是否存在?

1 个答案:

答案 0 :(得分:2)

属性BorderThicknessBorderBrushControl类中定义。

因此,您可以尝试将FrameworkElement投射到Control,如果可行,请设置属性:

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;
        }
    }
}