Controls Repositioning issue when using SharedSizeGroup in ItemsControl

Jul 11, 2011 at 6:51 AM

I have two item controls that are sharing the width using SharedSizeGroup. Now the issue is whenever i am updating the text inside the textbox,say the setting the text in the second row to 999999999 & then deleting the text,top controls are not repositioning themselves.

Here is my code :-

XAML :-

<Window x:Class="BindingGroupSample.Window2" 
   
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   
xmlns:local="clr-namespace:BindingGroupSample" 
   
Title="Window2" Height="300" Width="300"> 
<Window.Resources> 
   
<local:GroupNameGenerator x:Key="GroupNameGenerator1" /> 
   
<local:GroupNameGenerator x:Key="GroupNameGenerator2" /> 
</Window.Resources> 
<Grid> 
   
<StackPanel Grid.IsSharedSizeScope="True"> 
           
<ItemsControl Name="ItemsControl1"> 
           
<ItemsControl.ItemsPanel> 
               
<ItemsPanelTemplate> 
                   
<StackPanel Orientation="Horizontal" /> 
               
</ItemsPanelTemplate> 
           
</ItemsControl.ItemsPanel> 
           
<ItemsControl.ItemTemplate> 
               
<DataTemplate> 
                   
<Grid> 
                       
<Grid.ColumnDefinitions> 
                           
<ColumnDefinition SharedSizeGroup="{Binding Converter={StaticResource GroupNameGenerator1}}" /> 
                       
</Grid.ColumnDefinitions> 
                       
<TextBox Text="{Binding Index}" Background="Gray"/> 
                   
</Grid> 
               
</DataTemplate> 
           
</ItemsControl.ItemTemplate> 
       
</ItemsControl> 
           
<ItemsControl Name="ItemsControl2"> 
           
<ItemsControl.ItemsPanel> 
               
<ItemsPanelTemplate> 
                   
<StackPanel Orientation="Horizontal" /> 
               
</ItemsPanelTemplate> 
           
</ItemsControl.ItemsPanel> 
           
<ItemsControl.ItemTemplate> 
               
<DataTemplate> 
                   
<Grid> 
                       
<Grid.ColumnDefinitions> 
                           
<ColumnDefinition SharedSizeGroup="{Binding Converter={StaticResource GroupNameGenerator2}}" /> 
                       
</Grid.ColumnDefinitions> 
                           
<TextBox Text="{Binding Name}" /> 
                   
</Grid> 
               
</DataTemplate> 
           
</ItemsControl.ItemTemplate> 
       
</ItemsControl> 
   
</StackPanel> 
</Grid> 
</Window>  

Code:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 
 
namespace BindingGroupSample 
{ 
public partial class Window2 : Window 
{ 
   
ObservableCollection<Temp> list1 = new ObservableCollection<Temp>(); 
   
ObservableCollection<Temp> list2 = new ObservableCollection<Temp>();   
 
   
public Window2() 
   
{ 
       
InitializeComponent(); 
 
 
         
for (int i = 0; i < 25; i++)     
         
{ 
             list1
.Add(new Temp() { Index = i });    
             list2
.Add(new Temp() { Name = "AA" + i +i });       
         
}   
       
ItemsControl1.ItemsSource = list1;     
       
ItemsControl2.ItemsSource = list2;         
 
   
} 
} 
 
 
public class GroupNameGenerator : IValueConverter 
{ 
   
public Int32 Index { get; set; } 
   
public GroupNameGenerator() 
   
{ 
       
Index = 0; 
   
} 
 
   
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
   
{ 
       
return String.Format("Group{0}", ++Index); 
   
} 
   
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
   
{ 
       
throw new NotImplementedException(); 
   
} 
} 
 
 
public class Temp 
{ 
   
public int Index { get; set; } 
   
public string Name { get; set; } 
} 
} 

Please suggest something to achieve w/o taking any events to refresh the control items