开发者

How can get instance of class knowing its (string)name in runtime?

everyone. I operate with WP7. I have a ListBox.

Items in that ListBox must look very different. Each element is special.

What I did. I've created a simple dataTemplate for ListBoxItem. There is only ContentPresenter and nothing more.

And Content property ContentPresenter of is binding to DataContext.Content

Also I've created several userConrols that represent a special co开发者_如何转开发ntent for each element.

specialObject one = new specialObject();
one.textString = "one"; //data of object
oneButtonPanel b1 = new oneButtonPanel(); //look of object

specialObject two = new specialObject();
two.textString = "two"; //data of object
two_butonsPanel b2 = new two_butonsPanel(); //look of object

one.Content = b1; //set up Content property
b1.DataContext = one;
two.Content = b2;
b2.DataContext = two; //set up Content property        

ObservableCollection<specialObject> colItems = new ObservableCollection<specialObject>() { one, two };
myListBox.ItemsSource = colItems;

And everything works fine. But I don't know which UserControl I have to create actually.

two_butons b2 = new two_butons(); // Any other class can be here. I know only its string name - "two_butons"

How can I get a class instance, knowing only its name?

Or - can I get userControl instance in some other way?

Many thanks.


You can call assembly.GetType(someString), where assembly is the Assembly instance that contains the type. (eg, typeof(YourOtherType).Assembly)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜