我如何访问 gridIndex 的内容

发布于 2024-11-28 08:20:45 字数 4427 浏览 2 评论 0原文

我的网格有很多行和列。

看起来像这样。

<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 或 for :

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

我已经尝试了很多方法。但我没有这样做..很多人说“使用Grid.SetColumn和Grid.SetRow”,但它不能应用....

我该如何解决这个问题?

My Grid has many row and column.

It looks like this.

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

I want to initialize on code behind and use foreach or for on this code:

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

I have tried many ways. but i did't do it.. many people say"use Grid.SetColumn and Grid.SetRow" but it can't be applied....

How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2024-12-05 08:20:45

为了满足您的要求,可以按如下方式完成...

从后面的代码中,您可以在网格子项中添加 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);
                 }
            }
     }

但这样的设计效率并不高。它实际上应该是一个在其中托管多个 RectImg 控件的 ItemsControl。

像这样......

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

这里的“local”是MainWindow所在的本地项目命名空间的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;
           }
       }
    }

请告诉我这是否有帮助。

For the sake of your requirements this can be done as following...

From code behind you can add the RectImg controls in the grid children and before that make sure you assign the Row and Column indices.

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

But such design is not efficient. It should actually be an ItemsControl hosting multiple RectImg controls in it.

like this....

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

Here "local" is the XAML namespace of you local project namespace where MainWindow resides.

Your MainWindow should be as below...

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

Let me know if this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文