2011-07-11 25 views
0

我有兩個項目控件使用SharedSizeGroup共享寬度。 現在的問題是,當我更新文本框內的文本,比如將第二行中的文本設置爲999999999 &,然後刪除文本時,控件不會自行重新定位。控件在ItemsControl中使用SharedSizeGroup時的重新定位問題

這裏是我的代碼: -

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> 

代碼:

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; } 
} 
} 

請提出好的建議,以實現W/O採取任何事件刷新控制項目。

其實我要實現我的UI類似如下: -

   Item 1  Item 2  Item3 Item 4 
    Heading 1: AA  AA1  AA2  AA3 
    Heading 2: 20  10   11  89 
    Heading 3: 10  11   89  7 
    Heading 4: Expand  Expand Expand Expand 

也將有來自第1項水平滾動條到第4項可見的所有的時間。

回答

0

也許你應該換一種方式。讓我們用所有值填充一個列表,並在ItemTemplate中使用UniformGrid。

C#:

for (int i = 0; i < 25; i++) 
{ 
    list1.Add(new Temp() 
    { 
     Index = i, 
     Name = "AA" + i + i 
    }); 
} 
ItemsControl3.ItemsSource = list1; 

XAML:

<ItemsControl Name="ItemsControl3"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <UniformGrid Rows="2"> 
       <TextBox Text="{Binding Index}" Background="Gray" /> 
       <TextBox Text="{Binding Name}" /> 
      </UniformGrid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

我希望這是你需要的東西!

+0

非常感謝答覆...這是我需要的,但還有一個問題,我已經使用了擴展控制,所以如果我在擴展擴展器時採用均勻網格,所有行高都會增加。另外,我在左側有一個項目控件,爲每一行提供標題。我將左側項目控件的高度和SharedSizegroup的右側限制在一起。因此,如果使用統一網格,我無法正確對齊它們。請給我一些幫助相同.. – Dips

+0

用StackPanel替換UniformGrid以允許不同的行具有不同的高度。當然,它無法使用SharedSizeGroup在左邊對齊ItemsControl。但是看一看:行標題就像開頭的行值一樣。您可以在ItemTemplate的觸發器中使用AlternationIndex和AlternationCount來確定您的單元格是否是第一個,如果是,則將其渲染爲行標題。 –

+0

再次感謝...但是如果我使用改變索引,那麼我將如何讓我的滾動條從第二列開始,因爲第一列將成爲我的行標題...對不起再次提出太多問題並再次打擾你..我已經更新了我的問題,以添加我的UI的外觀,以更好地解釋它... – Dips