2016-08-03 77 views
0

视图我查询从多个数据库的一些数据,并将它们保存在一个二维字符串数组这样的:绑定二维字符串数组,以列出WPF

string[databaseID,fieldID] 

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

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

我想有一个字段名称的列和每个数据库ID的一列。 每一行应包含每个数据库的特定字段的数据。 它应该看起来像这样:

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

难道要创建一个包含Array-Data的新对象并简单地对它们进行分组会更容易吗?这可能会增加可读性 – lokusking

+0

你是什么意思? –

+0

你正在寻找的是一个'PivotGrid'。不幸的是,这不是内置的,但有很多公司在其框架中提供这种可能性(Telerik,Infragistics,DevExpress等)。不用说,你不会得到他们的免费:( – lokusking

回答

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的解决方案。您可以简单地按数据库进行分组和展开(或者只需更好地适应您的需求就可以将逻辑颠倒过来)。 请注意,我的DataObject与您的实际数据结构没有任何共同之处。我在大约7分钟内做了这个,它应该给你一个替代的看法。

干杯

+0

这其实并不坏。问题是,我在解释中忽略了一个方面来简化它。我想比较特定数据库的某些数据与所有其他数据库。因此,我想在该列式样视图中使用它。 Database1是一种主数据库,我想比较所有数据。 –