2

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

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

这样的事情存在吗?

4

1 回答 1

3

属性BorderThicknessBorderBrushControl类中定义。

因此,您可以尝试将您的FrameworkElementto 转换为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;
        }
    }
}
于 2012-11-11T09:56:03.357 回答