【问题标题】:Dynamic column binding动态列绑定
【发布时间】:2016-01-27 17:24:42
【问题描述】:

如何在网格中创建动态集列或者我应该使用什么网格模拟?

我有一个List,例如List<List<string>> = {{a},{b,c},{d,e},{f,g,h}} 我需要从每个元素中制作一行,并且应该拉伸子元素。如下图所示。因此,如果有一个元素,则占用所有网格宽度,如果有两个子元素,则每个子元素占用网格宽度的一半,依此类推。我从here 那里得到了 xaml

<Page>
   <Page.Resources>
        <DataTemplate x:Key="DataTemplate_Level2">
                <Button  Content="{Binding}" Margin="1,1,1,1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
        </DataTemplate>

        <DataTemplate x:Key="DataTemplate_Level1">
            <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource DataTemplate_Level2}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></StackPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </Page.Resources>

    <Grid>
        <ItemsControl ItemsSource="{Binding}" x:Name="lst" ItemTemplate="{StaticResource DataTemplate_Level1}"/>
    </Grid>
</Page>

但它不能像我需要的那样伸展子元素,像这样:

【问题讨论】:

    标签: c# xaml win-universal-app uwp


    【解决方案1】:

    试试这个:

    <ItemsControl HorizontalAlignment="Stretch">
      <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <ItemsControl ItemsSource="{Binding}" HorizontalAlignment="Stretch">
              <ItemsControl.ItemTemplate>
                <DataTemplate>
                  <Button Content="{Binding}" Margin="1,1,1,1" VerticalAlignment="Stretch" 
                          HorizontalAlignment="Stretch"/>
                </DataTemplate>
              </ItemsControl.ItemTemplate>
              <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                  <UniformGrid Rows="1" />
                </ItemsPanelTemplate>
              </ItemsControl.ItemsPanel>
            </ItemsControl>
          </DataTemplate>
        </ItemsControl.ItemTemplate>
    
      </ItemsControl>
    </ItemsControl>
    

    这就是它的样子

    编辑:

    由于 Universal App 不支持UniformGrid,我创建了自己的面板:

    public class UniformGridSingleLine : Panel
    {
      protected override Size MeasureOverride(Size availableSize)
      {
        foreach (UIElement child in Children)
          child.Measure(availableSize);
    
        return new Size(double.IsPositiveInfinity(availableSize.Width) ? 0 : availableSize.Width,
            double.IsPositiveInfinity(availableSize.Height) ? Children.Cast<UIElement>().Max(x => x.DesiredSize.Height) : availableSize.Height);
      }
    
      protected override Size ArrangeOverride(Size finalSize)
      {
        Size cellSize = new Size(finalSize.Width / Children.Count, finalSize.Height);
        int col = 0;
        foreach (UIElement child in Children)
        {
          child.Arrange(new Rect(new Point(cellSize.Width * col, 0), new Size(cellSize.Width, child.DesiredSize.Height)));
          col++;
        }
        return finalSize;
      }
    }
    

    只需将上面 XAML 中的 UniformGrid 替换为 UniformGridSingleLine(不要忘记命名空间)

    【讨论】:

    • 谢谢!但出现错误“UniformGrid 在 Windows 通用项目中不受支持...您确定它应该适用于 Windows 通用应用程序吗?
    • 抱歉,没看过。想到了WPF。我看看能不能找到替代品
    • 没有找到开箱即用的解决方案,但这篇文章可以帮助您创建自己的面板,其行为类似于 UniformGrid:blogs.microsoft.co.il/pavely/2012/02/07/…
    • 我已经创建了这样的课程,但页面保持空白。可能是因为背后的代码不同吗?你能告诉我你的代码吗?
    【解决方案2】:

    使用this post 解决的问题。

    【讨论】:

    • 仅链接的答案通常是 Stack Overflow 上的frowned upon。随着时间的推移,链接可能会萎缩并变得不可用,这意味着您的答案将来对用户毫无用处。如果您可以在实际帖子中提供答案的一般详细信息,并引用您的链接作为参考,那将是最好的。
    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 2011-05-19
    • 2016-02-04
    相关资源
    最近更新 更多