简体   繁体   中英

MouseLeftButtonDown event does not work Wpf

Good afternoon guys, my problem is this. I'm trying to make an event get fired after clicking the TextBox inside the DataTemplate, could anyone tell me why it did not fire the event?

Follow the code XAML below.

<StackPanel Orientation="Horizontal">
    <ItemsControl Grid.Row="0" ItemsSource="{Binding Pagamentos}"
            HorizontalAlignment="Left" x:Name="cbxFormaDePagamento" Margin="0,0,0,8">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox x:Name="txtFormaPagamento" Text="{Binding FormaPagamento.Nome}"
                        HorizontalContentAlignment="Left"
                        VerticalContentAlignment="Center" Width="216" Height="45" Background="White"
                        BorderThickness="1" BorderBrush="#b7b7b7" IsEnabled="False"
                        FontFamily="Roboto" FontSize="18" Foreground="Black"
                        Padding="0,0,0,0" Margin="0,0,0,8"
                        MouseLeftButtonDown="txtFormaPagamento_MouseLeftButtonDown"></TextBox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

The MouseLeftButtonDown function

private void txtFormaPagamento_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var viewModel = DataContext as FormasPagamentoViewModel;

    foreach (var currencyTextBox in FindVisualChildren<CurrencyTextBox>(this))
    {
        if (currencyTextBox.Name == "cbxValor")
        {
            currencyTextBox.Number = viewModel.TotalPagar;
        }
    }
}

Thank you!

The reason is because of order that the children in the ControlTemplate are added to the items control. You'll see that the TextBox is added to it first, and then the ContentPresenter is added, meaning that the ContentPresenter is on top of the TextBox. If you allow the ContentPresenter to participate in hit testing, then when you click on the TextBox, the ContentPresenter is eating the mouse event, and therefore the TextBox cannot see it.

So disable the IsHitTestVisible property of the content presenter of the items control. You can do so by applying a custom style to the items control.

Bypass this default operation by overriding the OnMouseDown method and after the base.OnMouseDown(e) command add the command: e.Handled = false; Of course this means that you have to create another textBox (eg TextBox0) inheriting from WPF TextBox and use the new one.

Public class TextBox0 : TextBox
{
    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        e.Handled = false;
    }
}

Optionally after coding your MouseLeftButtonDown event or the method OnMouseLeftButtonDown, and if you have another object, up the visual tree, acting unwantedly on this event, set this flag back to true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM