WPF TextBox DoubleClick Event Fires when using Scr

2019-07-10 04:16发布

问题:

I have a WPF TextBox, defined like this:

<TextBox Text="{Binding Path=/Comments}" 
    Margin="351,193.91,10,36" 
    x:Name="txtComments" 
    IsReadOnly="True" 
    VerticalScrollBarVisibility="Auto" 
    LostFocus="txtComments_LostFocus" 
    MouseDoubleClick="txtComments_MouseDoubleClick" 
    AcceptsReturn="True" />

This works exactly as I would like; however, when the VerticalScrollBars are visible, if you rapidly click the ScrollBar the txtComments_MouseDoubleClick event is fired. Is there any way I can change this behavior or detect that the event was fired by clicking the ScrollBar instead of the body of the textbox?

The main reason I want to do this, is that if you try to scroll down by double clicking the scroll bars the event is fired which causes the application to go down that path, which is very annoying if that is not the users intended action.

回答1:

In your double-click handler, check the OriginalSource property on the MouseButtonEventArgs. That source will tell you whether it was the actual scrollbar (the repeat button), or the textbox. Something like:

if (e.OriginalSource is TextBox)
{ 
    // Do your stuff.
}
else
{
    // From the scroll-bar.
}