动态地将RowDefinition的数量绑定到ItemsPanelTemplate中的Grid

时间:2017-04-26 07:00:02

标签: c# wpf xaml

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

     <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,如下所示

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

我已经在后端发现了RowDefinition需要的数量,如下所示

 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。我试过跟随一个但不适合我。评论如下,没有人回复。

How can I dynamically add a RowDefinition to a Grid in an ItemsPanelTemplate?

1 个答案:

答案 0 :(得分:1)

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

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