【问题标题】:How can I check if an object is a Rectangle or an Ellipse如何检查对象是矩形还是椭圆
【发布时间】:2015-05-01 12:18:31
【问题描述】:

如何检查创建为 Rectangle 或 Ellipse 的对象是否在 WPF 中?

Ellipse ellipse = new Ellipse();
Rectangle rect = new Rectangle(); 

if (rect.getType()){
    rect.Margin = new Thickness(x,y,0,0);
} else {
    ellipse.Margin = new Thickness(x, y, 0, 0);
}

【问题讨论】:

  • 这没有意义。 Margin 是 FrameworkElement 的一个属性,它是 Ellipse 和 Rectangle 的基类。你不需要那样的代码。

标签: c# wpf rectangles ellipse


【解决方案1】:

使用is keyword 可以检查对象的类型。

if(rect is Rectangle)

【讨论】:

    猜你喜欢
    • 2016-01-25
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2015-12-10
    • 1970-01-01
    • 2012-07-03
    相关资源
    最近更新 更多