DataGrid on TabControl column width sizing problem

Feb 6, 2009 at 3:39 AM

I have a usercontrol with a datagrid inside. Only code inside the usercontrol is initializing the datagrid. This usercontrol is in turn hosted inside a 2 tabitems. On running the app, the first tabitem's datagrid (inside the usercontrol) has correct column width but the 2nd tabitem's datagrid has different column width though they have the same code.

Here's the XAML for usercontrol

<UserControl
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="UntitledProject1.UserControl1"
 x:Name="UserControl"
 Width="640" Height="480" xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit">

 <Grid x:Name="LayoutRoot">
  <Custom:DataGrid x:Name="dgrd" IsSynchronizedWithCurrentItem="True" Margin="0,0,0,0"/>
 </Grid>
</UserControl>

and code behind


using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using Microsoft.Windows.Controls;

namespace UntitledProject1
{
    public partial class UserControl1
    {
        private bool _HasLoaded;
        private List<Person> _PersonList = new List<Person>();

        public UserControl1()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
            this.Loaded += new RoutedEventHandler(UserControl1_Loaded);
        }

        private void UserControl1_Loaded(object sender, RoutedEventArgs e)
        {
            if (!_HasLoaded)
            {
                this.Width = double.NaN;
                this.Height = double.NaN;
                InitGrid();
                _HasLoaded = true;
            }
        }

        private void InitGrid()
        {
            dgrd.AutoGenerateColumns = false;

            // Columns.
            DataGridTextColumn colAccount = new DataGridTextColumn();
            colAccount.Width = new DataGridLength(100, DataGridLengthUnitType.Star);
            dgrd.Columns.Add(colAccount);
            DataGridTextColumn colAmount = new DataGridTextColumn();
            colAmount.Width = new DataGridLength(50, DataGridLengthUnitType.Star);
            colAmount.Header = "Amount";

            dgrd.Columns.Add(colAmount);
            dgrd.ItemsSource = _PersonList;
        }

        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    }
}



XAML in window host

<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"
 x:Class="UntitledProject1.Window1"
 x:Name="Window"
 Title="Window1"
 Width="640" Height="480" WindowState="Maximized">

 <Grid x:Name="LayoutRoot">
  <TabControl x:Name="tabControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" IsSynchronizedWithCurrentItem="True">
   <TabItem x:Name="tabItem1" Header="TabItem"/>
   <TabItem x:Name="tabItem2" Header="TabItem">
    <Grid/>
   </TabItem>
  </TabControl>
 </Grid>
</Window>


code behind the window host

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace UntitledProject1
{
    public partial class Window1
    {
        public Window1()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {           
            tabItem1.Content = new UserControl1();
            tabItem2.Content = new UserControl1();
        }
    }
}


What's happening? If this is a bug, is there any workaround around it?