net自定义控制AddHandler不开火。

时间:2021-09-07 00:02:06

I am trying to make a custom control that is based on the textbox control. This textbox control contains a button, listbox, and popup as well. I try to add an event handler to the button's mouse down event in the OnApplyTemplate function of the class. When I step through debugging, the OnApplyTemplate is called and the event handler code is added.

我正在尝试创建一个基于文本框控件的自定义控件。这个文本框控件包含一个按钮、列表框和弹出窗口。我尝试在类的OnApplyTemplate函数中向按钮的鼠标向下事件添加事件处理程序。当我逐步调试时,调用OnApplyTemplate并添加事件处理程序代码。

My problem is that when I click the button the event handler sub DropDownButton_MouseDown is not called.

我的问题是,当我单击按钮时,事件处理程序sub DropDownButton_MouseDown并没有被调用。

here is my class:

这是我的类:

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

I am using wpf .net 4.5 in visual studio 2012. Here is my xaml for the custom control called AutoCompleteTextBox, the control is defined in another project within the solution:

我在visual studio 2012中使用wpf。net 4.5。这里是我的xaml,用于自定义控件AutoCompleteTextBox,该控件在解决方案中的另一个项目中定义:

<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>

Can someone please help me wire up this event handler so that when I click the button the sub DropDownButton_MouseDown is called.

有人能帮我把这个事件处理程序连接起来吗?这样当我点击按钮时,就会调用sub DropDownButton_MouseDown。

Thanks in advance

谢谢提前

1 个解决方案

#1


0  

So I CHanged the OnApplyTemplate to the following and it now works:

所以我将OnApplyTemplate修改为以下内容,它现在可以工作了:

Public Overrides Sub OnApplyTemplate()
    MyBase.OnApplyTemplate()

    '' if template is not nothing then initialize controls and wire up the event handlers
    If Me.Template IsNot Nothing Then

        InitializePopup()
        InitializeBorder()

        ''reset the handler for DropDownButton
        If DropDownButton IsNot Nothing Then
            RemoveHandler DropDownButton.Click, AddressOf DropDownButton_Click
        End If
        InitializeButton()
        If DropDownButton IsNot Nothing Then
            AddHandler DropDownButton.Click, AddressOf DropDownButton_Click
        End If

        ''reset handlers for listbox
        If ResultsListBox IsNot Nothing Then
            RemoveHandler ResultsListBox.PreviewMouseDown, AddressOf ResultListBox_PreviewMouseDown
            RemoveHandler ResultsListBox.KeyDown, AddressOf ResultListBox_KeyDown
        End If
        InitializeListbox()
        If ResultsListBox IsNot Nothing Then
            AddHandler ResultsListBox.PreviewMouseDown, AddressOf ResultListBox_PreviewMouseDown
            AddHandler ResultsListBox.KeyDown, AddressOf ResultListBox_KeyDown
        End If
    End If
End Sub

here is an example of the INitializeControl sub i wrote that is used above:

下面是我写的INitializeControl sub的一个例子:

Private Sub InitializeButton()
    If dropDownButton Is Nothing Then
        dropDownButton = TryCast(Me.Template.FindName("PART_DropDownButton", Me), Button)
    End If
End Sub

#1


0  

So I CHanged the OnApplyTemplate to the following and it now works:

所以我将OnApplyTemplate修改为以下内容,它现在可以工作了:

Public Overrides Sub OnApplyTemplate()
    MyBase.OnApplyTemplate()

    '' if template is not nothing then initialize controls and wire up the event handlers
    If Me.Template IsNot Nothing Then

        InitializePopup()
        InitializeBorder()

        ''reset the handler for DropDownButton
        If DropDownButton IsNot Nothing Then
            RemoveHandler DropDownButton.Click, AddressOf DropDownButton_Click
        End If
        InitializeButton()
        If DropDownButton IsNot Nothing Then
            AddHandler DropDownButton.Click, AddressOf DropDownButton_Click
        End If

        ''reset handlers for listbox
        If ResultsListBox IsNot Nothing Then
            RemoveHandler ResultsListBox.PreviewMouseDown, AddressOf ResultListBox_PreviewMouseDown
            RemoveHandler ResultsListBox.KeyDown, AddressOf ResultListBox_KeyDown
        End If
        InitializeListbox()
        If ResultsListBox IsNot Nothing Then
            AddHandler ResultsListBox.PreviewMouseDown, AddressOf ResultListBox_PreviewMouseDown
            AddHandler ResultsListBox.KeyDown, AddressOf ResultListBox_KeyDown
        End If
    End If
End Sub

here is an example of the INitializeControl sub i wrote that is used above:

下面是我写的INitializeControl sub的一个例子:

Private Sub InitializeButton()
    If dropDownButton Is Nothing Then
        dropDownButton = TryCast(Me.Template.FindName("PART_DropDownButton", Me), Button)
    End If
End Sub