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
  1. Home
  2. General Programming
  3. WPF
  4. TabControl with DataGrid. Columns won't size correctly

TabControl with DataGrid. Columns won't size correctly

Scheduled Pinned Locked Moved WPF
wpfhelptutorialcsharpquestion
6 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.
  • A Offline
    A Offline
    aalex675
    wrote on last edited by
    #1

    I am really hoping someone here can help me. I've been playing around with the WPF DataGrid and am having a weird problem. What I have is a TabControl with two tabs. Each tab has a DataGrid with two columns. The first column Width is "Auto" and the second column Width is "*" which should fill the remaining space in the Datagrid. What is happening is that the DataGrid in the first tab's columns are sized appropriately, but the second tab's second column is wider than it should be. Also, if I change the SelectedIndex to the second tab, it's columns are size correctly but the first tab's columns aren't. I have tried to Invalidate the DataGrid control when the selected tab changes but that didn't seem to do anything. I'm not sure how to explain this better but here is some example XAML:

    M 1 Reply Last reply
    0
    • A aalex675

      I am really hoping someone here can help me. I've been playing around with the WPF DataGrid and am having a weird problem. What I have is a TabControl with two tabs. Each tab has a DataGrid with two columns. The first column Width is "Auto" and the second column Width is "*" which should fill the remaining space in the Datagrid. What is happening is that the DataGrid in the first tab's columns are sized appropriately, but the second tab's second column is wider than it should be. Also, if I change the SelectedIndex to the second tab, it's columns are size correctly but the first tab's columns aren't. I have tried to Invalidate the DataGrid control when the selected tab changes but that didn't seem to do anything. I'm not sure how to explain this better but here is some example XAML:

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Well That's messed up :) Looking closely, it seems on the first switch to the other tab, its content (the datagrid) is sized to the entire tabcontrol size including the header... Then you can see the content shift right real quick as it gets positioned in the tab content area. From that point on, the content's size is incorrect, even if the tabcontrol is resized. The problem Can also be consistently seen at design time in both the Visual Studio designer and in Blend. What I can't figure out is a fix for this...I'll get back to you if I figure something out...

      aalex675 wrote:

      Sorry if the code is hard to read.

      Any code I can copy right into a test window and run with minimal changes is just fine with me. Your posted code was great. :) Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        Well That's messed up :) Looking closely, it seems on the first switch to the other tab, its content (the datagrid) is sized to the entire tabcontrol size including the header... Then you can see the content shift right real quick as it gets positioned in the tab content area. From that point on, the content's size is incorrect, even if the tabcontrol is resized. The problem Can also be consistently seen at design time in both the Visual Studio designer and in Blend. What I can't figure out is a fix for this...I'll get back to you if I figure something out...

        aalex675 wrote:

        Sorry if the code is hard to read.

        Any code I can copy right into a test window and run with minimal changes is just fine with me. Your posted code was great. :) Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        A Offline
        A Offline
        aalex675
        wrote on last edited by
        #3

        Great! Thanks for trying it. I'm glad it's not something really obvious that I missed at least.

        M 1 Reply Last reply
        0
        • A aalex675

          Great! Thanks for trying it. I'm glad it's not something really obvious that I missed at least.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          I didn't dig into the DataGrid source code, but this workaround seems to work... I added a SelectionChanged handler to the tab control:

              private void TabControl\_SelectionChanged(object sender, SelectionChangedEventArgs e)
              {
                  System.Collections.IEnumerable olditemssource = datagrid2.ItemsSource;
                  datagrid2.ItemsSource = null;
                  datagrid2.ItemsSource = olditemssource;// new MyItemCollection\_design();
              }
          

          Resetting the ItemsSource on the DataGrid is the only way (I could figure out) to get the DataGrid to re-layout its columns correctly. That's not really a good fix though, since if the user changes the column sort order, it won't be preserved if the tab is navigated away from and back to. They key point is to NOT set the ItemsSource until the DataGrid's associated tab is navigated to for the first time. I'll leave that as an exercise for you ;P I'm thinking this should probably be reported as a bug on the toolkit CodePlex site...

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            I didn't dig into the DataGrid source code, but this workaround seems to work... I added a SelectionChanged handler to the tab control:

                private void TabControl\_SelectionChanged(object sender, SelectionChangedEventArgs e)
                {
                    System.Collections.IEnumerable olditemssource = datagrid2.ItemsSource;
                    datagrid2.ItemsSource = null;
                    datagrid2.ItemsSource = olditemssource;// new MyItemCollection\_design();
                }
            

            Resetting the ItemsSource on the DataGrid is the only way (I could figure out) to get the DataGrid to re-layout its columns correctly. That's not really a good fix though, since if the user changes the column sort order, it won't be preserved if the tab is navigated away from and back to. They key point is to NOT set the ItemsSource until the DataGrid's associated tab is navigated to for the first time. I'll leave that as an exercise for you ;P I'm thinking this should probably be reported as a bug on the toolkit CodePlex site...

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            A Offline
            A Offline
            aalex675
            wrote on last edited by
            #5

            Thanks, I tried setting ItemsSource in code the first time each tab is selected and that takes care of the issue. I think this may already be in the bug tracker on CodePlex as issue #8842(DataGrid Star Column Widths incorrect when DataGrid was loaded with its Visibility not Visibile[^]) so I just voted for that issue. Thanks again for the help.

            M 1 Reply Last reply
            0
            • A aalex675

              Thanks, I tried setting ItemsSource in code the first time each tab is selected and that takes care of the issue. I think this may already be in the bug tracker on CodePlex as issue #8842(DataGrid Star Column Widths incorrect when DataGrid was loaded with its Visibility not Visibile[^]) so I just voted for that issue. Thanks again for the help.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              aalex675 wrote:

              I think this may already be in the bug tracker on CodePlex as issue #8842

              Cool! I didn't find it when I looked. Thanks! Cheers, Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              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