Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight中CaptureMouse()冒泡的事件_Silverlight_Draggable_Event Bubbling - Fatal编程技术网

Silverlight中CaptureMouse()冒泡的事件

Silverlight中CaptureMouse()冒泡的事件,silverlight,draggable,event-bubbling,Silverlight,Draggable,Event Bubbling,我正在用Silverlight编写一个窗口程序,这意味着弹出窗口的顶部栏有一个可拖动区域,在该可拖动区域内,有一个“X”关闭窗口。My drag函数有一个capturemouse()事件,当与发生的冒泡事件结合使用时,会阻止调用close函数。代码如下: private void close_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e){ pop.IsOpen =

我正在用Silverlight编写一个窗口程序,这意味着弹出窗口的顶部栏有一个可拖动区域,在该可拖动区域内,有一个“X”关闭窗口。My drag函数有一个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();
        }
private void close_MouseLeftButtonUp(对象发送器,System.Windows.Input.MouseButtonEventArgs e){
pop.IsOpen=false;
hasFocus=true;
}
私有void topBar_MouseLeftButtonDown(对象发送器,System.Windows.Input.MouseButtonEventArgs e)
{
边框项=发件人作为边框;
mouseY=e.GetPosition(null).Y;
mouseX=e.GetPosition(null).X;
draggable=true;
item.CaptureMouse();
}
私有void topBar_MouseMove(对象发送器,System.Windows.Input.MouseEventArgs e)
{
如果(可拖动){
双变量Y=e.GetPosition(null).Y-鼠标;
double changeX=e.GetPosition(null).X-mouseX;
double-top=changeY+(double)pop.GetValue(Canvas.TopProperty);
double left=changeX+(double)pop.GetValue(Canvas.LeftProperty);
如果(top450){
top=450;
}
pop.SetValue(Canvas.TopProperty,top);
SetValue(Canvas.LeftProperty,左);
mouseY=e.GetPosition(null).Y;
mouseX=e.GetPosition(null).X;
}
}
私有void topBar_MouseLeftButtonUp(对象发送器,System.Windows.Input.MouseButtonEventArgs e)
{
边框项=发件人作为边框;
可拖动=错误;
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>

该问题是由于您的MouseCapture调用引起的。设置mousecapture时,边框是唯一允许启动鼠标事件的控件。这意味着,当鼠标按钮按下时,图像不再触发mouseevents。 如果没有鼠标夹,它应该可以正常工作。只是出于好奇,你为什么设置并发布它

我希望这有帮助

编辑:

您可以获取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
        }
var x=e.GetPosition(close).x;
变量y=e.GetPosition(关闭).y;

如果(0),我假设“X”是一个按钮,那么当您单击该按钮时,边框仍然是事件的发送者?是否有“IshitteVisible”"在按钮上设置为false?我的代码中任何地方都没有声明
ishitestvisible
。您可以显示边框的xaml和“X”按钮吗?我只想看看定义了哪些事件。我添加了xaml,如果您还需要其他内容,请询问。是的,没有该选项,它可以正常工作,但我这样做的原因是因为我没有设置或者释放窗口拖动有问题。有没有办法确保首先调用关闭?边框和图像将始终位于同一位置,大小相同,因此您可以检查并查看鼠标坐标是否在图像内,并基于此调用关闭函数。