将二维字符串数组绑定到WPF中的列表视图

时间:2016-08-03 10:49:00

标签: c# arrays wpf data-binding

我查询来自多个数据库的一些数据并将它们保存在二维字符串数组中,如下所示:

string[databaseID,fieldID]

...其中fieldID = 0表示字段的名称。

数据库的数量各不相同,可以介于至少一个和未定义的数字之间。 确定字段数。

我希望字段名称有一列,每个databaseID有一列。 每行应包含每个数据库的特定字段的数据。 例如,它应该是这样的:

FieldName | Database1 | Database2 | Database3
----------|-----------|-----------|-----------
Name1     |     A     |     B     |     A
----------|-----------|-----------|-----------
Name2     |     1     |     2     |     3

我该怎么做?

为了创建列标题,我已经有了这个:

GridView gridView = new GridView();
gridView.AllowsColumnReorder = true;

GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("");
gvc1.Header = "Feldname";
gvc1.Width = 100;
gridView.Columns.Add(gvc1);
for (int i = 0; i < databases.Count; i++)
{
    GridViewColumn gvc = new GridViewColumn();
    gvc.DisplayMemberBinding = new Binding("Path=Row[" + (i + 1) + "]"); //Still not sure, wether this is okay
    gvc.Header = databases[i].DisplayName;
    gvc.Width = 100;
    gridView.Columns.Add(gvc);
}
lvBasic.View = gridView;

我所指的XAML部分相当简单:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <ListView Name="lvBasic">
    </ListView>
</ScrollViewer>

更新:我认为这不重要,所以我遗漏了问题的一个方面。我需要将database1的数据与所有其他数据库进行比较,因此需要此布局或其他适合该任务的布局。

1 个答案:

答案 0 :(得分:1)

这是解释我的评论的方法。

<强> XAML

<Grid >
        <Grid.Resources>
            <CollectionViewSource Source="{Binding DataObjects}" x:Key="CollectionViewSource" >
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="DatabaseId"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Grid.Resources>
        <ListView ItemsSource="{Binding Source={StaticResource CollectionViewSource}}">
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Expander IsExpanded="True">
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                                    <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                                    <TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>

            </ListView.GroupStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FieldId}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>           
    </Grid>

<强>数据对象

 public class DataObject {

    public string DatabaseId { get; set; }

    public string FieldId {
      get; set;
    }

  }

<强>用法

 this.DataObjects = new List<DataObject>();
      this.DataObjects.Add(new DataObject {DatabaseId = "Db1", FieldId = "FieldName"});
      this.DataObjects.Add(new DataObject { DatabaseId = "Db1", FieldId = "FieldFirstName" });
      this.DataObjects.Add(new DataObject { DatabaseId = "Db2", FieldId = "FieldName" });
      this.DataObjects.Add(new DataObject { DatabaseId = "Db2", FieldId = "FieldDate" });

      this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldName" });
      this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldDate" });
      this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldFirstName" });
      this.DataObjects.Add(new DataObject { DatabaseId = "Db3", FieldId = "FieldId" });

<强>结果 resultImage

这是一个100%MVVM-Conform解决方案。您可以简单地按数据库进行分组和扩展(如果更符合您的需求,只需反转逻辑)。 请注意,我的DataObject与您的实际数据结构没有任何共同之处。我在大约7分钟内制作了它,它应该给你一个替代视图。

干杯