2013-02-15 65 views
0

我有一个应该返回UIElementCollection的函数。该函数需要具有children属性的UIElement(即stackpanel,grid等),但不知道它是哪个UIElement,所以我将它存储到一个通用对象中。对象的子女

public UIElementCollection retCol (object givenObject){ 
... 
} 

我想返回givenObject的孩子,但我不能找到一种方法,除了铸造givenObject作为一个StackPanel或网格这样做。

有没有一种方法可以得到givenObject的儿童财产?

回答

0

StackPanelGrid无论从Panel继承,这样你就可以改变你的方法:

public UIElementCollection retCol (Panel givenObject){ 
    return givenObject.Children; 
} 

或者,如果你想提供给所有UIElement类型,你可以使它更通用和检查类型功能:

public UIElementCollection retCol (UIElement givenObject){ 
    if(givenObject is Panel) 
     return ((Panel)givenObject).Children; 
    else if(givenObject is SomeOtherContainer) 
     return ((SomeOtherContainer)givenObject).Children; 

    else 
     return null; 
}