级联组合框WPF mvvm和EF

时间:2015-06-09 20:32:39

标签: wpf entity-framework mvvm

我试图在我的视图中创建级联组合框。我有一个表,从SQL导入ADO.NET实体数据模型:

Partial Public Class tbl_Activities
Public Property MRU As String
Public Property Market As String
Public Property Tower As String
Public Property FirstActivity As String
Public Property SecondActivity As String

第一个组合框应该包含FirstActivity项,第二个组合项应该包含与第一个相关的SecondActivity项。

我使用方法来填充第一个:

    Public Sub FillFirstActivity()
    Using context As New TimerConn
        Dim result = (From act In context.tbl_Activities Where (act.MRU = MRU() And act.Market = MAR() And act.Tower = TOW()) Order By act.FirstActivity).Distinct().ToList()
            For Each act In result
                _First.Add(act)
            Next
    End Using
End Sub

属性:

    Public Property First() As ObservableCollection(Of tbl_Activities)
    Get
        Return _First
    End Get
    Set(value As ObservableCollection(Of tbl_Activities))
        _First = value
    End Set
End Property

Public Property Second() As ObservableCollection(Of tbl_Activities)
    Get
        Return _Second
    End Get
    Set(value As ObservableCollection(Of tbl_Activities))
        _Second = value
    End Set
End Property

Public Property SelectedFirst() As tbl_Activities
    Get
        Return _selectedFirst
    End Get
    Set(value As tbl_Activities)
        _selectedFirst = value
        Me.Second.Clear()
        Me.Second.Add(_selectedFirst)
    End Set
End Property

XAML:

<ComboBox ItemsSource="{Binding EmployeeViewM.First}" SelectedIndex="{Binding EmployeeViewM.SelectedFirst}" DisplayMemberPath="FirstActivity"/>
<ComboBox ItemsSource="{Binding EmployeeViewM.Second}" DisplayMemberPath="SecondActivity"/>

我的问题

我无法正确填充它。在第一个组合框中,我有两倍(Test1,Test2,Test2),在第二个组合框中,我只获得与从第一个组合框中选择的项目相关的项目: enter image description here

第一个应该是不同的,第二个应该包含当前活动的所有项目。

First Combobox =&#39; Test1&#39;,&#39; Test2&#39;

第二个组合框=如果&#39;测试1&#39;那么&#39; Test1Add&#39;,如果&#39; Test2&#39;那么&#39; Test2Add&#39;,&#39; Test2Add1&#39;

我不知道我在这里做错了什么。也许我应该用PK-FK将它分成两个表然后尝试?如果是这样,我怎么能将它绑定到组合框?

我的表格视图:

enter image description here

非常感谢您的任何建议。

0 个答案:

没有答案