WPF VB.NET ComboBox DataBinding

时间:2016-06-15 20:57:18

标签: wpf vb.net data-binding combobox

我是LINQ to SQL和WPF的新手,我正在使用多个在线指南和YouTube视频来选择它(以及此应用程序)。

问题:

在加载窗口时(以及刷新Windows DataContext的其他关键数据更改),我需要更新ComboBox所显示的选项以及具有来自Window DataContext绑定的匹配ID的项目(c_Instance.FK_RiskIndicator_ID)< / p>

我从一个视图中填充ComboBox,该视图返回显示字段和ID(所有ID都是GUID)。

当选择更改时,我调用存储过程以使用所选项目的ID更新数据库。

Private db As New AppDatabase_DBDataContext
Private CRCTypeView As BindingListCollectionView
Private InstanceDetailTypeView As BindingListCollectionView
Property guInstance_ID As Guid

Private Sub Refresh_PCA_Instance()
    Dim c_Instance As IEnumerable(Of t_Instance)
    c_Instance = (From Inst In db.t_Instances Where Inst.ID = guInstance_ID Select Inst)

    If c_Instance.Count = 1 Then
        Me.DataContext = c_Instance
        Me.InstanceDetailTypeView = CType(CollectionViewSource.GetDefaultView(Me.DataContext), BindingListCollectionView)
    ElseIf c_Instance.Count > 1 Then
        Me.DataContext = c_Instance.First
        Me.InstanceDetailTypeView = CType(CollectionViewSource.GetDefaultView(Me.DataContext), BindingListCollectionView)
    End If
End Sub

Private Sub LoadCRCType()
    Dim CRCTypeList As IEnumerable(Of vw_RiskIndicator)
    CRCTypeList = (From RiskIndicator In db.vw_RiskIndicators
                   Order By RiskIndicator.IndexNum
                   Select RiskIndicator)

    PCA_CRC_ComboBox.DataContext = CRCTypeList
    Me.CRCTypeView = CType(CollectionViewSource.GetDefaultView(PCA_CRC_ComboBox.DataContext), BindingListCollectionView)
End Sub

Private Sub PCA_CRC_ComboBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles PCA_CRC_ComboBox.SelectionChanged
    If PCA_CRC_ComboBox.SelectedItem IsNot Nothing Then
        db.sp_Instance_RiskIndicator_UPSERT(guInstance_ID, PCA_CRC_ComboBox.SelectedValue)
        Refresh_PCA_Instance() 'Refreshes the Window DataContext
    End If
End Sub

ComboBox的XAML:

<ComboBox x:Name="PCA_CRC_ComboBox" Grid.Row="1" Grid.Column="2"
    ItemsSource="{Binding}"
    SelectedValuePath="ID"
    DisplayMemberPath="RiskIndicator"
    PreviewKeyDown="RiskIndicator_ComboBox_PreviewKeyDown"/>

我原以为我可以通过绑定SelectedItem来做到这一点,但我无法理解。我原以为它会像:

<ComboBox x:Name="PCA_CRC_ComboBox" Grid.Row="1" Grid.Column="2"
    SelectedItem="{Binding Path=FK_RiskIndicator_ID}" <!--THIS LINE HERE-->
    ItemsSource="{Binding}"
    SelectedValuePath="ID"
    DisplayMemberPath="RiskIndicator"
    PreviewKeyDown="RiskIndicator_ComboBox_PreviewKeyDown"/>

非常感谢任何帮助。

编辑:将代码组合在一起。我认为这就是我的意思。我个人认为这种布局如果不那么解释。

0 个答案:

没有答案