Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. WPF: Add Controls To Grid At Runtime

WPF: Add Controls To Grid At Runtime

Scheduled Pinned Locked Moved WPF
csharpcsswpfquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • K Kevin Marois

      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

      J Offline
      J Offline
      Jurgen Rohr
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups