简体   繁体   English

如何在WPF中将对象数据类型转换为UIElement

[英]how to convert an object data type into UIElement in WPF

I declared an object data type 我声明了一个对象数据类型

object itemize;

then i created a method with conditions, depending on the condition, the itemize will be assigned into a corresponding UserControl 然后我创建了一个带有条件的方法,根据条件,将逐项分配给相应的UserControl

public void StackFilter(string mode)
{
    this.stkFilter.Children.Clear();

    switch(mode)
    {
        case "FilterWithType":
            itemize = new FilterWithTypesUC();
            break;
        case "FilterBasic":
            itemize = new FilterBasicUC();
            break;
    }
    this.stkFilter.Children.Add(itemize);
}

it says I can't add the itemize as the stack panel's child because it is not an UIElement type. 它说我不能将itemize添加为堆栈面板的子项,因为它不是UIElement类型。 Ho how do I make it as UIElement? 我怎么做它作为UIElement?

将元素转换为UIElement

this.stkFilter.Children.Add(itemize as UIElement);

I'll suggest you to declare it as UIElement itself. 我建议您将其声明为UIElement本身。 Then you don't need a cast. 然后,您不需要演员。

UIElement itemize;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM