2017-03-02 102 views
1

這裏對WPF非常陌生。我會設置場景: 我有與支出的東西像這樣的列表一個ListView:在列表視圖中填充組合框列表

  • 費用說明:超市購物
  • 費用日期:2017年2月3日
  • 費用金額:$ 50
  • 費用類型:食品雜貨

的費用類型存儲在一個單獨的表在我的數據庫,並通過外鍵expense_type_id掛鉤的費用。

我試圖得到沒有費用類型的費用列表,並允許用戶從選項列表(即費用類型表中的數據)中設置費用類型。

爲此,我創建了一個ListView,其中包含費用而沒有expense_type_id。

<ListView x:Name="lvAllocation" HorizontalAlignment="Left" Height="475" Margin="0,44,0,0" VerticalAlignment="Top" Width="1308" FontSize="16"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Posted" Width="120" DisplayMemberBinding="{Binding ex_posted_date}" /> 
      <GridViewColumn Header="Trans" Width="120" DisplayMemberBinding="{Binding ex_trans_date}" /> 
      <GridViewColumn Header="Description" Width="120" DisplayMemberBinding="{Binding ex_description}" /> 
      <GridViewColumn Header="Amount" Width="120" DisplayMemberBinding="{Binding ex_amount}" /> 
      <GridViewColumn Header="Expense Type" x:Name ="cmbExpenseType" Width="120"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" /> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView.View> 
</ListView> 

上面是詳細列表視圖的WPF。填充後面的代碼是:

Private Sub SetupAllocationTab() 
    'Get list from DB (where there isn't an expense type) 
    Dim queryExceptions = 
     From expense In dataEntities.expenses 
     Where expense.expense_type.ex_type_desc Is Nothing 
     Order By expense.ex_posted_date 
     Select 
      expense.ex_id, 
      expense.ex_type_id, 
      expense.override_ex_type_id, 
      expense.month_id, 
      expense.account_id, 
      expense.ex_posted_date, 
      expense.ex_trans_date, 
      expense.ex_description, 
      expense.ex_amount, 
      expense.ex_deduction, 
      expense.notes, 
      expense.ex_file_desc, 
      expense.insert_datetime, 
      expense.insert_user, 
      expense.insert_process, 
      expense.update_datetime, 
      expense.update_user, 
      expense.update_process 

    'For each item, make an expense object and add it to the ListView 
    For Each item In queryExceptions.ToList 
     Dim exp = New expense(item.ex_id, item.ex_type_id, item.override_ex_type_id, item.month_id, item.account_id, item.ex_posted_date, item.ex_trans_date, item.ex_description, item.ex_amount, 
            item.ex_deduction, item.notes, item.ex_file_desc, item.insert_datetime, item.insert_user, item.insert_process, item.update_datetime, item.update_user, item.update_process) 
     lvAllocation.Items.Add(exp) 
    Next 

    'Create list for expense dropdown 
    ExceptionTypeDropdown() 

End Sub 

運行從我的數據庫獲取的edmx數據模型。

我的問題是,如何使用表expense_type中存在的dat填充ComboBox列。這就像我在嘗試一些我在網上找到的東西一樣。任何幫助深表感謝。

Private Sub ExceptionTypeDropdown() 

    Dim expenseTypes = 
    From expense_type In dataEntities.expense_type 
    Select expense_type.ex_type_code, 
      expense_type.ex_type_desc 

    'Add the expense type combo box on the DataGrid 


End Sub 

Expense List Screenshot

回答

0

請注意我的代碼是C#比VB.Net多,但我想你可以翻譯這一部分。

所以,你需要的是一個獨立的項目源與所有可能的費用類型和所選值綁定到您的收藏:

在你的控制/窗口的XAML資源:

<CollectionViewSource x:Key="expenseTypesSource"/> 

在後面的初始化代碼:

var expenseTypesSource = Resources["expenseTypesSource"] as CollectionViewSource; 
expenseTypesSource.Source = expenseTypes; // each item as { ex_type_code, ex_type_desc } 

組合框用法:

<ComboBox 
    ItemsSource="{Binding Source={StaticResource expenseTypesSource}}" 
    DisplayMemberPath="ex_type_desc" 
    SelectedValuePath="ex_type_code" 
    SelectedValue="{Binding ex_type_id}" 
    Width="120" /> 

正確使用DataContext後,您可以跳過部分代碼,但我不打算在這裏詳細討論。