2016-08-26 30 views
1

我需要創建一個DataGrid,顯示包括他們收入的客戶列表。我有兩個DataTable(客戶/收入),我將它們合併爲一個查詢,結果是客戶/收入項目列表。DataGrid:對於「組」項目有一個穩定/固定的行

我現在想要在一個表格中顯示所有客戶,包括他們的收入,但是不會爲每個收入行復制/顯示每個客戶。所以我想在客戶繪製一次時隱藏所有其他客戶圖紙。

客戶行應始終位於頂部。排序應該是可能的。換句話說,我正在尋找一個沒有團體的分組功能。

Regular display when opening for the first time

它不應該發生在客戶的列,它看起來像這樣:

enter image description here

另一種選擇可能是一個組,但我希望有相同的「列「像分組元素中的調整行爲一樣顯示。

任何想法都將非常受歡迎。

+0

請發表您的XAML代碼。 –

回答

0

您可以使用DatagridRowTemplate來實現此目的。

您的DataGridDataGridRowTemplate中將有兩列,您有一個ListBox顯示單個客戶的所有收入。

您的XAML代碼將如下所示。

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Customers}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Name" Binding="{Binding CustomerName}" /> 
     <DataGridTextColumn Header="Birthday" Binding="{Binding CustomerDOB}" /> 
    </DataGrid.Columns> 
    <DataGrid.RowDetailsTemplate> 
     <DataTemplate> 
      <ListBox ItemsSource="{Binding Incomes}" /> 
     </DataTemplate> 
    </DataGrid.RowDetailsTemplate> 
</DataGrid> 

假設你有一個名爲List/ObservableCollectionCustomers。並且CustomerName,CustomerDOBIncomes(客戶收入列表)是您的客戶類名稱。

+0

感謝您的回覆。我已經嘗試過,但我希望收入不在細節中,但在主網格中。事實上,收入的信息比這裏顯示的要大得多。有大約20個項目給客戶。而且,在RowDetails中,我沒有可能調整列的大小,而且我還需要rowdetails來獲取其他信息。 – msedi

3

您可以用持有DataGrid的模板自己替換Income列的CellTemplate。

pic unsorted

pic sorted

XAML

<DataGrid.Columns> 
    <DataGridTextColumn Header="Customer" Binding="{Binding Customer}" > 
    </DataGridTextColumn> 
    <DataGridTextColumn Header="Birth Date" Binding="{Binding Date}" > 
    </DataGridTextColumn> 
    <DataGridTemplateColumn Header="Income" > 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <DataGrid ItemsSource="{Binding IncomeList}" 
            AutoGenerateColumns="False" 
            HeadersVisibility="None" 
            CanUserAddRows="False" 
            CanUserDeleteRows="False"> 
        <DataGrid.Columns> 
         <DataGridTextColumn 
             Width="*" 
             IsReadOnly="True" 
             Binding="{Binding Path=.}"> 
         </DataGridTextColumn> 
        </DataGrid.Columns> 
       </DataGrid> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns> 
+1

謝謝。我也嘗試了這個,有一個網格在網格解決方案。問題是DataGrid的行爲絕對怪異。這裏有幾個帖子。一個問題是mousehweel無法正常工作,另一個問題是,滾動不能正確更新。 – msedi