0
我正在嘗試使基於文本框控件的自定義控件。該文本框控件也包含一個按鈕,一個列表框和一個彈出框。我嘗試在類的OnApplyTemplate函數中向按鈕的鼠標向下事件添加事件處理程序。當我逐步調試時,將調用OnApplyTemplate並添加事件處理程序代碼。wpf vb.net自定義控件AddHandler沒有觸發
我的問題是,當我點擊按鈕的事件處理程序子DropDownButton_MouseDown不會被調用。
這裏是我的類:
Imports System.Windows.Controls.Primitives
<TemplatePart(Name:="PART_ControlBorder", Type:=GetType(Border))> _
<TemplatePart(Name:="PART_DropDownButton", Type:=GetType(Button))> _
<TemplatePart(Name:="PART_Popup", Type:=GetType(Popup))> _
<TemplatePart(Name:="PART_ListBox", Type:=GetType(ListBox))> _
Public Class AutoCompleteTextBox
Inherits TextBox
#Region "DECLARATIONS"
Private Mainborder As Border
Private popup As Popup
Private listBox As ListBox
Private dropDownButton As Button
#End Region
#Region "METHODS"
Private Sub PopupOpen()
If popup IsNot Nothing And popup.IsOpen = False Then
popup.IsOpen = True
Else
Return
End If
End Sub
Private Sub DropDownButton_MouseDown(sender As Object, e As System.EventArgs)
PopupOpen()
End Sub
#End Region
#Region "APPLY TEMPLATE"
Public Overrides Sub OnApplyTemplate()
MyBase.OnApplyTemplate()
If Me.Template IsNot Nothing Then
Dim button__1 As Button = TryCast(Me.Template.FindName("PART_DropDownButton", Me), Button)
If button__1 IsNot dropDownButton Then
'First unhook existing handler
If dropDownButton IsNot Nothing Then
RemoveHandler dropDownButton.MouseDown, AddressOf DropDownButton_MouseDown
End If
dropDownButton = button__1
If dropDownButton IsNot Nothing Then
AddHandler dropDownButton.MouseDown, AddressOf DropDownButton_MouseDown
End If
End If
End If
End Sub
#End Region
#Region "CONSTRUCTOR"
Sub New()
DefaultStyleKeyProperty.OverrideMetadata(GetType(AutoCompleteTextBox), New FrameworkPropertyMetadata(GetType(AutoCompleteTextBox)))
End Sub
#End Region
End Class
我使用WPF .NET 4.5在Visual Studio 2012年。這裏是我的叫AutoCompleteTextBox自定義控件XAML,控制在解決方案中的另一個項目定義:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:krisis="clr-namespace:Krisis.Controls;assembly=Krisis.Controls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<krisis:AutoCompleteTextBox SearchText="Bob" Width="200" MinHeight="35" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
能有人幫我線了此事件處理程序,這樣,當我按一下按鈕子DropDownButton_MouseDown被調用。
在此先感謝