WPF: Add Controls To Grid At Runtime
-
I'm in a WPF app, and I want to add controls to a grid at runtime. I have:
private void LoadView(ViewModelBase View)
{
Type ViewType = View.GetType();switch (ViewType.Name.ToLower()) { case "groupviewmodel": CurrentView = new GroupViewModel(); CurrentControl = new crlGroupView(); break; case "ruleviewmodel": CurrentView = new RuleViewModel(); CurrentControl = new crlRuleTree(); break; } CurrentControl.DataContext = CurrentView; grdMain.Children.Add(CurrentControl); Grid.SetRow(CurrentControl, 1); Grid.SetColumn(CurrentControl, 0); grdMain.Children.Add(CurrentControl);
}
The control does not appear. Anyone know what's wrong?
Everything makes sense in someone's mind
-
I'm in a WPF app, and I want to add controls to a grid at runtime. I have:
private void LoadView(ViewModelBase View)
{
Type ViewType = View.GetType();switch (ViewType.Name.ToLower()) { case "groupviewmodel": CurrentView = new GroupViewModel(); CurrentControl = new crlGroupView(); break; case "ruleviewmodel": CurrentView = new RuleViewModel(); CurrentControl = new crlRuleTree(); break; } CurrentControl.DataContext = CurrentView; grdMain.Children.Add(CurrentControl); Grid.SetRow(CurrentControl, 1); Grid.SetColumn(CurrentControl, 0); grdMain.Children.Add(CurrentControl);
}
The control does not appear. Anyone know what's wrong?
Everything makes sense in someone's mind
Hi, don't know, if this one's still open, but if so, I can give you two hints: 1. your code isn't working. You can't add one control two times (results in an ArgumentException) 2. if you replace the beginning of your method (I mean everything until but not including "Grid.SetRow(...") with
Button CurrentControl = new Button();
CurrentControl.Content = "Hello World";the Button shows up as expected and everything here is fine. The error your seeking seems to lurk in the code not provided. Cheers Jürgen