2011-08-04 62 views
0

我的網格有很多行和列。我如何訪問gridIndex的內容

它看起來像這樣。

<Grid x:Name="grid" Grid.ColumnSpan="3" RenderTransformOrigin="0.5,0.5"> 
     <Grid.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform/> 
       <SkewTransform/> 
       <RotateTransform/> 
       <TranslateTransform/> 
      </TransformGroup> 
     </Grid.RenderTransform> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
     </Grid.ColumnDefinitions> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="0"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="1"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="2"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="3"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="4"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="5"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="6"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="7"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="8"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="9"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="10"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="11"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="12"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="13"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="14"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="0" Grid.Column="15"></rectImg:RectImg> 


     <rectImg:RectImg Grid.Row="1" Grid.Column="0"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="2" Grid.Column="0"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="3" Grid.Column="0"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="4" Grid.Column="0"></rectImg:RectImg> 
     <rectImg:RectImg Grid.Row="5" Grid.Column="0"></rectImg:RectImg> 

    </Grid> 

我想在初始化代碼後面使用的foreach或在此代碼:

<rectImg:RectImg Grid.Row="0" Grid.Column="0"></rectImg:RectImg> 

我已經嘗試了很多方法。但我沒做到..很多人說「使用Grid.SetColumn和Grid.SetRow」,但它不能應用....

我該如何解決這個問題?

回答

0

對於您的需求着想這是可以做到如下...

從後面的代碼,你可以在網格孩子添加RectImg控制和之前確保你指定行和列索引。

 private void AddRectImgControls() 
    { 
      var rectImg = new RectImg(); 
      for(var i = 0; i++; i < this.grid.RowDefinitions.Count) 
      { 
       for(var j = 0; j++; j < this.grid.ColumnDefinitions.Count) 
       { 
        var rectImg = new RectImg(); 
        Grid.SetRow(rectImg, i); 
        Grid.SetColumn(rectImg, j); 
        this.grid.Children.Add(rectImg); 
       } 
      } 
    } 

但是這樣的設計效率不高。它實際上應該是一個ItemsControl託管多個RectImg控件。

這樣....

<ItemsControl ItemsSource="{x:Static local:MainWindow.UniformGridCollection}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
        <UniformGrid Rows="8" Columns="23" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <rectImg:RectImg/>  
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

這裏的「地方」是的,你的XAML命名空間的本地項目的命名空間,其中主窗口所在。

你的主窗口應該如下...

public partial class MainWindow : Window 
    { 
     //// .... rest of the MainWindow code. 

     public static List<object> UniformGridCollection 
     { 
      get 
      { 
       var coll = new List<object>(); 
       for(i = 0; i < 8 * 23; i++) //// 8 and 23 are UniformGrid's row and column size. 
       { 
        coll.Add(new object()); 
       } 
       return coll; 
      } 
     } 
    } 

讓我知道,如果這有助於。

+0

它的工作原理!謝謝!!!!!!!! –

+0

很高興我能幫到你。 –