2016-08-03 82 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是一種主數據庫,我想比較所有數據。 –