0

我有一个包含 TextBox 的用户控件,我想捕获 mousedown 事件但是,我似乎无法让事情正常工作!我现有的非工作代码如下,任何帮助将不胜感激。

用户控件 xaml:

<UserControl x:Class="LeftLabel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Width="auto" Height="auto" >
    <StackPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=TextBlockText}"
                       Name="UcTextBlock"
                       Width="{Binding Path=TextBlockWidth}"
                       FontSize="{Binding Path=TextBlockFontSize}"
                       HorizontalAlignment="Right"
                       TextAlignment="Right"
                       VerticalAlignment="Center"
                       Margin="5,0,0,0" />
            <TextBox Text="{Binding Path=TextBoxText}"
                     Name="UcTextBox"
                     MouseDown="UcTextBox_MouseDown" 
                     Width="{Binding Path=TextBoxWidth}"
                     Height="{Binding Path=TextBoxHeight}"
                     FontSize="{Binding Path=TextBoxFontSize}"
                     Padding="{Binding Path=TextBoxPadding}"
                     Margin="5,0,0,0" 
                     BorderThickness="0" />
        </StackPanel>
    </StackPanel>
</UserControl>

用户控件.vb:

Public Event TextBoxMouseDown As EventHandler
Private Sub UcTextBox_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles UcTextBox.MouseDown
    RaiseEvent TextBoxMouseDown(sender, e)
End Sub

出于测试目的,我以编程方式将 UserControls 添加到我的 MainWindow 中:

Dim count As Integer = 1
While count < 10
    Dim ucl As New LeftLabel
    With ucl
        .Margin = New Thickness(4)
        .TextBlockText = "Label " & count.ToString
        .TextBlockWidth = 100
        .TextBlockFontSize = 12
        .TextBoxFontSize = 12
        .TextBoxHeight = 20
        .TextBoxText = "Initial Text " & count.ToString
        .TextBoxPadding = New Thickness(2)
        .TextBoxWidth = 150
        AddHandler .TextBoxMouseDown, AddressOf LabelLeftTextBoxMouseDown
    End With
    TextBoxStackPanel.Children.Add(ucl)
    count += 1
End While

Private Sub LabelLeftTextBoxMouseDown(sender As Object, e As EventArgs)
    Dim txt As TextBox = DirectCast(sender, TextBox)
    MsgBox(txt.Text)
End Sub
4

1 回答 1

3

这是一个有点普遍的问题。

由于这些控件在内部处理这些事件,因此某些控件会发生这种情况。例如,按钮“吞下”了点击,而是公开了它自己的事件——点击事件。如果您想在 XAML 中声明您的文本框事件处理程序,我建议您检查 Preview*-events(即 PreviewMouseDown),这些总是会发生,如果您需要对点击做出反应,也许这可以解决您的问题。

<TextBox Text="{Binding Path=TextBoxText}"
                     Name="UcTextBox"
                     PreviewMouseDown="UcTextBox_PreviewMouseDown"
                     Width="{Binding Path=TextBoxWidth}"
                     Height="{Binding Path=TextBoxHeight}"
                     FontSize="{Binding Path=TextBoxFontSize}"
                     Padding="{Binding Path=TextBoxPadding}"
                     Margin="5,0,0,0" 
                     BorderThickness="0" />
于 2013-07-09T15:20:47.407 回答