WPF网格动态列(wpf grid with dynamic columns)

2019-07-04 21:55发布

我有我想绑定到一个WPF网格的集合。

我现在面临的问题是,列的数量是动态的,是依赖于一个集合。 下面是一个简单模拟了起来:

public interface IRows
{
    string Message{get;}
    IColumns[] Columns{get;}
}

public interface IColumns
{
    string Header {get;}
    AcknowledgementState AcknowledgementState{get;}
}

public interface IViewModel
{
    ObservableCollection<IRows> Rows {get;}
}

我想我的看法绑定到行集合,其中包含列的集合。

我的列集合包含应该由图像(1 3的可能性)来表示一个枚举。 它还包含应该只显示在一列(静态和只是一些文本信息)消息属性。 它还包含应被显示作为该列的标题页眉字符串。

需要注意的是列数为变量(在头被设置为确认的那一刻,但这种情况将会改变以表示动态数据)。

更新:这是瑞秋实施建议后

    <ItemsControl
 ItemsSource="{Binding Items, Converter={StaticResource PresentationConverter}}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Grid ShowGridLines="true"
         local:GridHelpers.RowCount="{Binding RowCount}"
         local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemContainerStyle>
    <Style>
      <Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
      <Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
    </Style>
  </ItemsControl.ItemContainerStyle>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <ContentControl Content="{Binding}">
        <ContentControl.Resources>
          <DataTemplate DataType="{x:Type UI:MessageEntity}">
            <TextBox Text="{Binding Message}"></TextBox>
          </DataTemplate>
          <DataTemplate DataType="{x:Type UI:StateEntity}">
            <TextBox Text="{Binding State}"></TextBox>
          </DataTemplate>
        </ContentControl.Resources>
      </ContentControl>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

这几乎给了我什么,我现在想。 我只坚持我应该为标题做什么。 任何建议都欢迎。

Answer 1:

您可以使用嵌套ItemsControls

这里有一个简单的例子:

<!-- Bind Rows using the default StackPanel for the ItemsPanel -->
<ItemsControl ItemsSource="{Binding Rows}">
    <!-- Set the Template for each row to a TextBlock and another ItemsControl -->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <!-- Need to set Width of name TextBlock so items line up correctly -->
                <TextBlock Width="200" Text="{Binding Name}" />

                <ItemsControl ItemsSource="{Binding Columns}">
                    <!-- Use a horizontal StackPanel to display columns -->
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


Answer 2:

使用网格的做法可能会使事情复杂得多,他们应该是。 您是否尝试过改变ListView的模板,或者使用DataGrid作此用途?

举一个例子,看看这个项目: http://www.codeproject.com/Articles/25058/ListView-Layout-Manager

或者这一个: http://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView

如果用网格去,我相信你会不得不增加大量的代码背后的管理行和列,它们的大小,细胞内容物的量......而一个ListView /数据网格可以让你做到这一点通过动态模板。



Answer 3:

创建使用代码网格如在所示http://msdn.microsoft.com/en-us/library/system.windows.controls.grid(v=vs.90).aspx#feedback

创建类型ColumnDefinition的属性,(和使用属性改变)来创建列。



Answer 4:

还有使用动态对象来创建你列的选项。 这是一个有点费力,但结果是非常有效的和一般的解决方案是非常灵活。

这将显示的基础,以动态对象绑定DynamicObject与自动列生成一个DataGrid?

我不得不使用它与嵌套对象,有对象,然后试图单元格内容的对象绑定列了一些麻烦。

下面是我对如何做到这一点的例子提出了一个问题

问题WPF DataGridCell的XAML内容绑定到



文章来源: wpf grid with dynamic columns