WPF / VB ComboBox SelectionChanged引发两次

时间:2016-01-07 16:21:46

标签: wpf vb.net combobox

我是WPF的新手。我正在研究的项目要求使用VB作为语言和平面文件作为数据(我不能改变要求)。我只发现了(2)关于WPF与VB的书籍与C中的几十本相比,网络搜索也很少。希望有人在VB体验中对这个问题有一些了解。

这是页面上的级联事件。我加载了一个名为unit的组合框。当以单位进行选择时,它使设施组合框可用,并根据所选的单位值加载数据。如果设备再次更改,它将清除设施框并根据新的单位值重新加载。

我的代码如下。让我感到困惑的是设施组合框的负载结果。它被加载,好像平面文件的读取循环两次。在函数的顶部是用于在加载组合框之前清除列表的代码。因此,即使函数被调用两次,代码应该在技术上清除列表,加载,再次清除列表然后加载。我输入了一系列弹出窗口来确认SelectionChange被调用两次,因此调用了两次加载工具。

我查看了发送者和e参数,两个调用中它们是相同的,所以我不确定如何捕获和停止第二个调用或甚至触发它的内容。

哦,弹出窗口发生了一件非常奇怪的事情。它们现在被放置在VB代码中,使问题消失。我只在组合框中看到一次设施。如果我拿走弹出窗口,我会再次出现两次。

有人看到我遗失的东西吗?在此先感谢您的审核和协助。

XAML:     

<Grid  x:Name="gridTreat1" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock x:Name="lblTreat1Title" HorizontalAlignment="Left" Margin="22,20,0,0" TextWrapping="Wrap" Text="Treatment Details     1 of 3" VerticalAlignment="Top" Width="459" FontSize="36" FontWeight="Bold"/>
    <TextBox x:Name="lblUnit" Text="Unit:" HorizontalAlignment="Left" Margin="66,95,0,0" VerticalAlignment="Top" Width="141" FontSize="18" Height="34" BorderBrush="White" IsReadOnly="True" TabIndex="0" IsTabStop="False"/>
    <ComboBox x:Name="cboUnit" HorizontalAlignment="Left" Margin="234,95,0,0" VerticalAlignment="Top" Width="586" Height="34" FontSize="18" SelectionChanged="cboUnit_SelectionChanged">
    </ComboBox>
    <TextBox x:Name="lblFacility" Text="Facility:" HorizontalAlignment="Left" Margin="66,202,0,0" VerticalAlignment="Top" Width="141" FontSize="18" Height="34" BorderBrush="White" IsReadOnly="True" TabIndex="0" IsTabStop="False"/>
    <ComboBox x:Name="cboFacility" HorizontalAlignment="Left" Margin="234,202,0,0" VerticalAlignment="Top" Width="586" Height="34" FontSize="16" SelectionChanged="cboFacility_SelectionChanged">
    </ComboBox>
    <TextBox x:Name="lblPhysician" Text="Physician:" HorizontalAlignment="Left" Margin="66,256,0,0" VerticalAlignment="Top" Width="141" FontSize="18" Height="34" BorderBrush="White" IsReadOnly="True" TabIndex="0" IsTabStop="False"/>
    <ComboBox x:Name="cboPhysician" HorizontalAlignment="Left" Margin="234,256,0,0" VerticalAlignment="Top" Width="586" Height="34" FontSize="16">
    </ComboBox>
    <TextBox x:Name="lblStep2" Text="------------------------------------------------------------------------------------------------------" HorizontalAlignment="Left" Margin="66,153,0,0" VerticalAlignment="Top" Width="754" Height="34" FontSize="18" FontWeight="Bold" BorderBrush="White" IsReadOnly="True" TabIndex="0" IsTabStop="False"/>
    <Button x:Name="cmdCancel" Content="Cancel" HorizontalAlignment="Left" Margin="104,502,0,0" VerticalAlignment="Top" Width="165" Height="50" FontSize="18" />
    <Button x:Name="cmdNext" Content="NEXT &gt;&gt;" HorizontalAlignment="Left" Margin="618,502,0,0" VerticalAlignment="Top" Width="165" Height="50" FontSize="18"/>
    <Rectangle x:Name="recPatient" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="57" Margin="544,18,0,0" Stroke="Black" VerticalAlignment="Top" Width="329"/>
    <TextBox x:Name="lblPRN" Text="PRN:" HorizontalAlignment="Left" Margin="550,20,0,0" VerticalAlignment="Top" Width="42" Height="20" Background="#FFF4F4F5" BorderBrush="#FFF4F4F5" IsReadOnly="True" IsTabStop="False" TabIndex="0"/>
    <TextBox x:Name="lblPatName" Text="Patient:" HorizontalAlignment="Left" Margin="550,41,0,0" VerticalAlignment="Top" Width="70" Height="20" Background="#FFF4F4F5" BorderBrush="#FFF4F4F5" FontSize="14" IsReadOnly="True" IsTabStop="False" TabIndex="0"/>
    <TextBox x:Name="txtPRN" HorizontalAlignment="Left" Height="11" Margin="625,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235" Background="#FFF4F4F5" BorderBrush="#FFF4F4F5" FontWeight="Bold" FontSize="14" IsReadOnly="True" IsTabStop="False" TabIndex="0"/>
    <TextBox x:Name="txtPatName" HorizontalAlignment="Left" Height="27" Margin="625,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235" Background="#FFF4F4F5" BorderBrush="#FFF4F4F5" FontWeight="Bold" FontSize="14" IsReadOnly="True" IsTabStop="False" TabIndex="0"/>
    <TextBox Text="CONFIDENTIAL PEER REVIEW PRIVILEGED" HorizontalAlignment="Left" Margin="290,558,0,0" VerticalAlignment="Top" FontWeight="Bold" Foreground="Red" BorderBrush="White" FontSize="14" IsTabStop="False" TabIndex="0" IsReadOnly="True"/>
    <TextBox x:Name="txtTest" HorizontalAlignment="Left" Margin="423,399,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
    <TextBox x:Name="txtTest2" HorizontalAlignment="Left" Margin="423,436,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
</Grid>

VB代码背后:

    Private Sub cboUnit_SelectionChanged(sender As System.Object, e As SelectionChangedEventArgs) Handles cboUnit.SelectionChanged
    Dim myUnit As String = ""
    Dim cb As ComboBox
    Dim cbi As ComboBoxItem
    cb = CType(cboUnit, ComboBox)
    cbi = CType(cb.SelectedItem, ComboBoxItem)
    myUnit = cbi.Tag.ToString
    Select Case GlobalVariable.gvAction
        Case "A"
            Call Load_cboFacility(myUnit)
            Me.cboFacility.Visibility = Xaml.Visibility.Visible
            Me.lblFacility.Visibility = Xaml.Visibility.Visible
        Case "E"
            Call Load_cboFacility(myUnit,"2")
    End Select
End Sub

Private Async Sub Load_cboFacility(ByVal myUnit As String, Optional myIsSel As String = "")
    Dim nextline As String = ""
    Dim myTag As String = ""
    Dim myKey As String = ""
    Dim myContent As String = ""
    Dim localfolder As Windows.Storage.StorageFolder = Windows.Storage.ApplicationData.Current.LocalFolder
    Dim myfile As StorageFile = Await localfolder.GetFileAsync("dicFacility.txt")

    Dim cb As ComboBox
    Dim cbi As ComboBoxItem
    Dim myMsg2 As New MessageDialog("1")
    Await myMsg2.ShowAsync

    cb = CType(cboFacility, ComboBox)
    cb.Items.Clear()

    Using reader As StreamReader = New StreamReader(Await myfile.OpenStreamForReadAsync)
        Dim myMsg As New MessageDialog("2")
        Await myMsg.ShowAsync
        nextline = Await reader.ReadLineAsync
        While (nextline <> Nothing)
            myKey = nextline.Substring(0, 2)
            myKey = myKey.Trim
            If myUnit = myKey Then
                myTag = nextline.Substring(7, 4)
                myTag = myTag.Trim
                myContent = nextline.Substring(18, nextline.Length - 18)
                cbi = New ComboBoxItem
                cbi.Tag = myTag
                cbi.Content = myContent
                If myIsSel <> "" Then
                    If myIsSel = myTag Then
                        cbi.IsSelected = True
                    End If
                End If
                cb.Items.Add(cbi)
            End If
            nextline = Await reader.ReadLineAsync
        End While
    End Using

End Sub

对问题的修订:

我终于让代码工作,作为我认为Rachel建议我做的变化。它不是优雅而是功能。如果某人有更清洁的方式,我愿意接受建议:

通过添加DropDownClosed事件修改XAML:

<ComboBox x:Name="cboUnit" HorizontalAlignment="Left" Margin="234,95,0,0" VerticalAlignment="Top" Width="586" Height="34" FontSize="18" SelectionChanged="cboUnit_SelectionChanged" DropDownClosed="cboUnit_Closed" />

修改后的VB代码:

在我的constants.vb文件中,我创建了一个名为gvUnitCount的新变量,并且最初设置为零。当下拉关闭时,我的计数重置为零。当选择改变时,它会运行一次,如果递增更高,IF语句会将你踢出去。

Private Sub cboUnit_Closed(sender As System.Object, e As Object) Handles cboUnit.DropDownClosed
    GlobalVariable.gvUnitCount = 0
End Sub

Private Sub cboUnit_SelectionChanged(sender As System.Object, e As SelectionChangedEventArgs) Handles cboUnit.SelectionChanged
    Dim myUnit As String = ""
    Dim cbUnit2 As ComboBox
    Dim cbiUnit2 As ComboBoxItem
    cbUnit2 = CType(cboUnit, ComboBox)
    cbiUnit2 = CType(cbUnit2.SelectedItem, ComboBoxItem)
    myUnit = cbiUnit2.Tag.ToString
    Me.txtUnitTag.Text = cbiUnit2.Tag.ToString

    If GlobalVariable.gvUnitCount = 0 Then
        Select Case GlobalVariable.gvAction
            Case "A"
                Me.cboFacility.Visibility = Xaml.Visibility.Visible
                Me.lblFacility.Visibility = Xaml.Visibility.Visible
                Call Load_cboFacility(myUnit)
                Me.cboPhysician.Visibility = Xaml.Visibility.Visible
                Me.lblPhysician.Visibility = Xaml.Visibility.Visible
                Call Load_cboPhysician(myUnit)
            Case "E"
                Call Load_cboFacility(myUnit)
                Call Load_cboPhysician(myUnit)
        End Select
        GlobalVariable.gvUnitCount = GlobalVariable.gvUnitCount + 1
    End If

End Sub
像我说的那样,不是很漂亮而是功能。我一直只能通过这种方式获得一个装置组合框。

0 个答案:

没有答案