在Silverlight中使用CaptureMouse()冒泡的事件

时间:2011-06-24 15:36:57

标签: silverlight draggable event-bubbling

我正在Silverlight中编写一个窗口程序,这意味着弹出窗口的顶部栏有一个可拖动区域,在该可拖动区域内,一个“X”关闭窗口。我的拖动函数有一个capturemouse()事件,当与发生的事件冒泡结合时,会阻​​止调用close函数。这是代码:

private void close_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e){
            pop.IsOpen = false;
            hasFocus = true;
        }

        private void topBar_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Border item = sender as Border;
            mouseY = e.GetPosition(null).Y;
            mouseX = e.GetPosition(null).X;
            draggable = true;
            item.CaptureMouse();

        }

        private void topBar_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if(draggable){
                double changeY = e.GetPosition(null).Y - mouseY;
                double changeX = e.GetPosition(null).X - mouseX;
                double top = changeY + (double)pop.GetValue(Canvas.TopProperty);
                double left = changeX + (double)pop.GetValue(Canvas.LeftProperty);
                if(top<0){
                    top = 0;
                }
                if(left<0){
                    left = 0;
                }
                if(left>670){
                    left = 670;
                }
                if(top>450){
                    top = 450;
                }
                pop.SetValue(Canvas.TopProperty, top);
                pop.SetValue(Canvas.LeftProperty, left);
                mouseY = e.GetPosition(null).Y;
                mouseX = e.GetPosition(null).X;
            }
        }

        private void topBar_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Border item = sender as Border;
            draggable = false;
            mouseY = -1;
            mouseX = -1;
            item.ReleaseMouseCapture();
        }

编辑:这是整个弹出窗口的XAML:

<Popup x:Name="pop" Height="200" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center">
                    <Border CornerRadius="5" Width="200" Height="200" Background="#FFFAFCFF" BorderThickness="1">
                        <Border.BorderBrush>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#99666666" Offset="0" />
                                <GradientStop Color="#99F5F5F5" Offset="0.5"/>
                                <GradientStop Color="#99666666" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.BorderBrush>
                        <StackPanel>
                            <Border x:Name="topBar" CornerRadius="4,4,0,0" BorderBrush="Silver" BorderThickness="0,0,0,1" Background="Crimson" Width="198" Height="20" MouseLeftButtonDown="topBar_MouseLeftButtonDown" MouseMove="topBar_MouseMove" MouseLeftButtonUp="topBar_MouseLeftButtonUp">
                                <Image x:Name="close" Source="X.png" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,7,0" Height="11" Width="11" MouseLeftButtonUp="close_MouseLeftButtonUp" />
                            </Border>
                            <StackPanel Margin="10">
                                <TextBlock Text="Printer info goes here..." />
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </Popup>

1 个答案:

答案 0 :(得分:1)

由于您的MouseCapture调用而出现问题。设置mousecapture时,边框是唯一允许启动鼠标事件的控件。这意味着图像在鼠标按钮关闭时不再触发鼠标事件。 没有鼠标捕获它应该工作正常。仅仅因为我的好奇心,你为什么要设置并发布它?

我希望这会有所帮助。

编辑:

您可以获取mouseEvent的位置,看看它是否属于图像:

        var x = e.GetPosition(close).X;
        var y = e.GetPosition(close).Y;
        if (0 <= x && x <= 11 && 0 <= y && y <= 11)
        {
            //do the close call
        }

我没有测试过这段代码,但它应该接近你想要做的。