事件冒泡问题 (ScrollViewer)

发布于 2024-09-08 06:00:56 字数 1499 浏览 6 评论 0原文

我对冒泡事件有疑问。我设法在边框、网格、堆栈面板中冒泡事件,但不在 ScrollViewer 中

如果您查看下面的示例,您会注意到,当您单击 TextBlock 时,事件会冒泡到网格。但是当我包含 ScrollViewer 时,事件停止在这里并且不会发送到网格。

现在有人知道为什么会发生这种情况吗?是否可以解决?我确实需要能够通过 ScrollViewer 冒泡事件,因为我一直使用它。

<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <!--<ScrollViewer MouseLeftButtonDown="ScrollViewer_MouseLeftButtonDown">-->
            <StackPanel Orientation="Vertical" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                <TextBlock Text="Click me to bubble an event" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
            </StackPanel>
        <!--</ScrollViewer>-->
</Grid>


public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("LayoutRoot clicked");
    }


    private void ScrollViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("ScrollViewer clicked");
        e.Handled = false;
    }

    private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("StackPanel clicked");
        e.Handled = false;
    }

    private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Textblock clicked");
        e.Handled = false;
    }
}

I have a problem with bubbeling events. I manage to bubble events in borders, grid, stackpanel, but not in a ScrollViewer

If you look at the example below you will notice that when you click the TextBlock the event is bubbeled up to the Grid. But when I include the ScrollViewer the event stops here and is not sent up to the Grid.

Does anyone now whay this happends and if it can be fixed? I really need to be able to bubble events through a ScrollViewer as I use it all the time.

<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <!--<ScrollViewer MouseLeftButtonDown="ScrollViewer_MouseLeftButtonDown">-->
            <StackPanel Orientation="Vertical" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
                <TextBlock Text="Click me to bubble an event" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
            </StackPanel>
        <!--</ScrollViewer>-->
</Grid>


public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("LayoutRoot clicked");
    }


    private void ScrollViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("ScrollViewer clicked");
        e.Handled = false;
    }

    private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("StackPanel clicked");
        e.Handled = false;
    }

    private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Textblock clicked");
        e.Handled = false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪失控 2024-09-15 06:00:56

使用 AddHandler(yourDelegate, True);添加事件处理程序的语法,这将忽略可视化树中其他控件设置的 Handled 标志。

use AddHandler(yourDelegate, True); syntax for adding event handlers, which will ignore Handled flag set by other controls in the visual tree.

錯遇了你 2024-09-15 06:00:56

我遇到了这个问题,user572559 发布的修复解决了我的问题。对于那些需要它的人,下面是我所做的(为发布而修改):

_scrollViewer = new ScrollViewer();
_scrollViewer.AddHandler(
    ScrollViewer.MouseLeftButtonDownEvent, 
    new MouseButtonEventHandler(OnMouseLeftButtonDown),
    true);
_scrollViewer.AddHandler(
    ScrollViewer.MouseLeftButtonUpEvent, 
    new MouseButtonEventHandler(OnMouseLeftButtonUp), 
    true);

...

    void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ...
    }

    void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        ...
    }

另请注意,如果您处理这些,您也可能正在处理 MouseMove。 MouseMove 为我工作,不需要这样做,而且它似乎也不支持这种方式(不是冒泡事件)。

I had this problem and the fix posted by user572559 fixed my issue. For those that need it, below is what I did (modified for posting):

_scrollViewer = new ScrollViewer();
_scrollViewer.AddHandler(
    ScrollViewer.MouseLeftButtonDownEvent, 
    new MouseButtonEventHandler(OnMouseLeftButtonDown),
    true);
_scrollViewer.AddHandler(
    ScrollViewer.MouseLeftButtonUpEvent, 
    new MouseButtonEventHandler(OnMouseLeftButtonUp), 
    true);

...

    void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ...
    }

    void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        ...
    }

Also note that if you hare handling these you may be handling MouseMove as well. MouseMove worked for me without needing to do this, and it also does not seem to be supported in this fashion (not a bubbling event).

巨坚强 2024-09-15 06:00:56

您可以通过重写 ScrollViewer 来阻止 MouseButtonEventArgs 上的 e.Handled,如下所示

public sealed class ClickScrollViewer : ScrollViewer
{
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        this.Focus();
    }
}

You can prevent e.Handled on MouseButtonEventArgs by overriding ScrollViewer like this

public sealed class ClickScrollViewer : ScrollViewer
{
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        this.Focus();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文