将 RowDefinition 的数量动态绑定到 ItemsPanelTemplate 中的网格 [英] Bind dynamically number of RowDefinition to a Grid in an ItemsPanelTemplate

查看:30
本文介绍了将 RowDefinition 的数量动态绑定到 ItemsPanelTemplate 中的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了 xaml,用于在 3 列网格中显示视频文件.我使用的 xaml 如下:

I have written xaml for display video files in a grid with 3 column. I have used xaml as below:

     <ItemsControl  Name="icTodoList" Grid.IsSharedSizeScope="True" ItemsSource="{Binding items}" Grid.Row="0" Grid.Column="0" >
                        <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate >
                                    <Grid  x:Name="icTooList" Margin="100,0,100,0" Style="{Binding Path=Style}">
                                    <Grid.ColumnDefinitions>
                                        <!--Whatever I do I can't get the screen to resize and the cols to have the same width-->
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="A" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="A" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="A" />
                                    </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
                                    </Grid.RowDefinitions>
                                </Grid>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="40,0,40,30">
                                    <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0">
                                           <Hyperlink TextDecorations="None" NavigateUri="{Binding UriPath}" RequestNavigate="Hyperlink_RequestNavigate"
                                                      CommandParameter="{Binding ElementName=myImg}">
                                                            <Image Width="120" Height="120"  x:Name="myImg" Source="{Binding Source}" Margin="5"/>
                                           </Hyperlink>
                                    </TextBlock>
                                    <TextBlock Margin="0,120,0,0" HorizontalAlignment="Center">
                                                <TextBlock FontSize="20px" Text="{Binding Title}" Foreground="white"></TextBlock>
                                        </TextBlock>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemContainerStyle>
                            <Style>
                                <Style.Setters>
                                    <Setter Property="Grid.Row" Value="{Binding GridRow}" />
                                    <Setter Property="Grid.Column" Value="{Binding GridColumn}" />
                                </Style.Setters>
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                    </ItemsControl>

这里我使用了如下的 RowDefinition

Here I have used RowDefinition as below

<Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
                                    </Grid.RowDefinitions>

我已经发现后端需要的 RowDefinition 数量如下

I have already found numbers of RowDefinition need in backend as below

 void setaligned()
            {
                int currentColumn = 0;
                int currentRow = 0;

                foreach (BindingFilesContent checkBoxItem in items)
                {
                    checkBoxItem.GridColumn = currentColumn;
                    checkBoxItem.GridRow = currentRow;
                    if (currentColumn != 2)
                    {
                        currentColumn++;
                    }
                    else
                    {
                        currentRow++;
                        currentColumn = 0;
                    }
            }
}

但是我需要在Grid中动态绑定这个RowDefinition.我试过跟随一个但不适合我.评论下面的问题没有人回答.

But I need to bind this RowDefinition dynamically in Grid. I have tried with following one but not working for me. Commented on below question no one replied.

我该怎么做向 ItemsPanelTemplate 中的 Grid 动态添加 RowDefinition?

推荐答案

我使用了 UniformGrid 并绑定了行,找到下面的代码来动态绑定行.

I have used the UniformGrid and bind the rows, find below code to bind rows dynamically.

Xaml:

 <UniformGrid Columns="3" Rows="{Binding RowCount}">
  </UniformGrid>

C#

List<PartList> items1 = new List<PartList>();

 int currentRow = 10;

 items1.Add(new PartList() { RowCount = currentRow });

public class PartList
{
            public int RowCount { get; set; }
}

这篇关于将 RowDefinition 的数量动态绑定到 ItemsPanelTemplate 中的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆