I have WPF project that I use for test purposes, for every Window I create I add new button in the start window, I want to change this and add the buttons dynamically by reflection, I tried this:
public partial class _DynamicWindow : Window
{
StackPanel stack = new StackPanel();
public _DynamicWindow()
{
InitializeComponent();
RetrieveAll();
}
private void RetrieveAll()
{
foreach (var window in Assembly.GetAssembly(typeof(Window)).GetTypes())
{
CreateButton(window);
}
}
private void CreateButton(Type window)
{
var obj = Activator.CreateInstance(window) as Window;
Button btnShow = new Button();
btnShow.Click += delegate { obj.Show(); };
btnShow.Content = obj.Name;
stack.Children.Add(btnShow);
}
}
but I get this error:
System.Windows.Markup.XamlParseException: ''The invocation of the constructor on type 'WPFTest._DynamicWindow' that matches the specified binding constraints threw an exception.' Line number '6' and line position '9'.'
ArgumentException: Cannot create an instance of <>f__AnonymousType0`1[j__TPar] because Type.ContainsGenericParameters is true.
Aucun commentaire:
Enregistrer un commentaire