VB.NET WPF 列表框中的选定项 - 无法多次触发选定项
VB.NET WPF Selected Item in ListBox - Can't trigger selected item more than once
我正在将 UserControl ucRegisterItem 加载到名为 MainGrid 的列表框中。
ucRegisterItem 有一个唯一的 ID,当在 MainGrid 中单击该项目时,我需要能够检索该 ID。
使用下面的代码,当加载 ucRegisterItem 的实例时,我可以单击每个项目一次并检索 ID - 但如果我再次单击它,则没有任何反应。我可以添加一个新的 ucRegisterItem 并获取该 ID,但同样,只需一次。
我不确定我做错了什么。
这是 UC ucRegisterItem 代码:
Public theOID As Integer
Public Event OIDSelected(ByVal OID As Integer)
Public Sub New(ByVal OID As Integer, ByVal Seat As Integer, ByVal Item As String, ByVal Qty As Double, ByVal Price As Double, ByVal SalesTax As Double, ByVal Total As Double, ByVal Optional Notes As String = "")
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
theOID = OID
theSeat.Text = theOID 'Seat
theProdName.Text = Item
theQty.Text = Qty
thePrice.Text = FormatCurrency(Price, 2)
theTax.Text = FormatCurrency(SalesTax, 2)
theTotal.Text = FormatCurrency(Total, 2)
If Notes = "" Then
ItemInfo.Children.Remove(ItemNotes)
Else
ActualNotes.Text = Notes
End If
End Sub
Private Sub whatever_Selected(sender As Object, e As RoutedEventArgs) Handles whatever.Selected
RaiseEvent OIDSelected(theOID)
End Sub
End Class
这是我将其加载到我的列表框中的位置:
Public Sub AddListItem(ByVal OID As Integer, ByVal Seat As Integer, ByVal Item As String, ByVal Qty As Double, ByVal Price As Double, ByVal SalesTax As Double, ByVal Total As Double, ByVal Optional Notes As String = "")
Dim newItem As New ucRegisterItem(OID, Seat, Item, Qty, Price, SalesTax, Total, Notes)
newItem.Name = "item" & OID
MainGrid.Items.Add(newItem)
MainGrid.SelectedIndex = MainGrid.Items.Count - 1
MainGrid.ScrollIntoView(MainGrid.SelectedItem)
AddHandler newItem.OIDSelected, AddressOf ShoutItOut
End Sub
Public Sub ShoutItOut(sender As Object)
'Setting the value of this mug.
selectedVal = CInt(sender)
MsgBox("Value: " & sender.ToString)
End Sub
我添加了 MsgBox 以查看它会发送什么。我让 MsgBox 显示一次,但在第二次点击或对该特定项目之后的任何点击时都不显示。
如有任何帮助,我们将不胜感激!
谢谢!
奥斯塔斯说得对。您订阅的是 'Selected' 而不是 'Clicked',因此请将您的事件处理程序更改为 MouseUp 或 Click 事件。
我正在将 UserControl ucRegisterItem 加载到名为 MainGrid 的列表框中。 ucRegisterItem 有一个唯一的 ID,当在 MainGrid 中单击该项目时,我需要能够检索该 ID。
使用下面的代码,当加载 ucRegisterItem 的实例时,我可以单击每个项目一次并检索 ID - 但如果我再次单击它,则没有任何反应。我可以添加一个新的 ucRegisterItem 并获取该 ID,但同样,只需一次。
我不确定我做错了什么。
这是 UC ucRegisterItem 代码:
Public theOID As Integer
Public Event OIDSelected(ByVal OID As Integer)
Public Sub New(ByVal OID As Integer, ByVal Seat As Integer, ByVal Item As String, ByVal Qty As Double, ByVal Price As Double, ByVal SalesTax As Double, ByVal Total As Double, ByVal Optional Notes As String = "")
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
theOID = OID
theSeat.Text = theOID 'Seat
theProdName.Text = Item
theQty.Text = Qty
thePrice.Text = FormatCurrency(Price, 2)
theTax.Text = FormatCurrency(SalesTax, 2)
theTotal.Text = FormatCurrency(Total, 2)
If Notes = "" Then
ItemInfo.Children.Remove(ItemNotes)
Else
ActualNotes.Text = Notes
End If
End Sub
Private Sub whatever_Selected(sender As Object, e As RoutedEventArgs) Handles whatever.Selected
RaiseEvent OIDSelected(theOID)
End Sub
End Class
这是我将其加载到我的列表框中的位置:
Public Sub AddListItem(ByVal OID As Integer, ByVal Seat As Integer, ByVal Item As String, ByVal Qty As Double, ByVal Price As Double, ByVal SalesTax As Double, ByVal Total As Double, ByVal Optional Notes As String = "")
Dim newItem As New ucRegisterItem(OID, Seat, Item, Qty, Price, SalesTax, Total, Notes)
newItem.Name = "item" & OID
MainGrid.Items.Add(newItem)
MainGrid.SelectedIndex = MainGrid.Items.Count - 1
MainGrid.ScrollIntoView(MainGrid.SelectedItem)
AddHandler newItem.OIDSelected, AddressOf ShoutItOut
End Sub
Public Sub ShoutItOut(sender As Object)
'Setting the value of this mug.
selectedVal = CInt(sender)
MsgBox("Value: " & sender.ToString)
End Sub
我添加了 MsgBox 以查看它会发送什么。我让 MsgBox 显示一次,但在第二次点击或对该特定项目之后的任何点击时都不显示。
如有任何帮助,我们将不胜感激!
谢谢!
奥斯塔斯说得对。您订阅的是 'Selected' 而不是 'Clicked',因此请将您的事件处理程序更改为 MouseUp 或 Click 事件。