在ListView

时间:2017-03-02 09:44:21

标签: wpf vb.net listview

这里的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

1 个答案:

答案 0 :(得分:0)

请注意我的代码比VB.Net更多C#,但我想你可以翻译这部分。

因此,您需要的是一个独立的物品来源,其中包含所有可能的费用类型以及绑定到您的收藏中的选定值:

在控件/窗口的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后,您可以跳过部分代码,但我不会在此处详细介绍。