Silverlight鼠标左键向下问题
在Silverlight 3项目中,我在边框控件上使用鼠标左键向下事件,但发现即使在稍微超出边框(如5-10像素)的地方单击按钮,事件也会引发Silverlight鼠标左键向下问题,silverlight,silverlight-3.0,mouseevent,mouseleftbuttondown,Silverlight,Silverlight 3.0,Mouseevent,Mouseleftbuttondown,在Silverlight 3项目中,我在边框控件上使用鼠标左键向下事件,但发现即使在稍微超出边框(如5-10像素)的地方单击按钮,事件也会引发 <Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2"> <Grid x:
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>
以前有人遇到过这个问题吗
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>
期待中的感谢
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>
以下是我们捕获brdSelect事件的XAML:
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>
据我所知,问题在于
MouseLeftButtonDown
事件处理。在MSDN中,我发现:
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>
MouseLeftButtonDown事件是一个
冒泡事件。这意味着如果
多个MouseLeftButtonDown事件
是为元素树定义的,
事件由每个对象接收
在对象层次结构中,从
直接接收数据的对象
事件,然后向每个
连续的父元素。
冒泡的比喻表明
事件从底部开始并开始工作
它在对象树的上方。暂时
冒泡事件,发送方参数
标识发生事件的对象
是被处理的,不一定是对象
实际收到输入的
引发事件的条件
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>
您有一个带有
CornerRadius=“5”
的子边框。可能该属性实际上会导致子边框在视觉上不应发生鼠标事件时接收鼠标事件。尽管这取决于实际实现,您最好检查一下。您确定没有应用边距,或者边框内的内容没有负边距吗?共享您的XAML可能会有所帮助。我尝试过使用brdImage和层次结构中的所有其他控件,但都不起作用。你知道我应该找什么吗?
<Border MouseLeftButtonDown="Grid_Mousedown" x:Name="brdSelect" Grid.Row="1" Grid.Column="1" BorderThickness="0.5" BorderBrush="#FF2D9FD2">
<Grid x:Name="grdSelect" >
<Border x:Name="brdImage" BorderThickness="0" CornerRadius="5" Width="Auto" Height="Auto">
<Image Height="Auto" Width="Auto" Stretch="UniformToFill" x:Name="imgMotive"/>
</Border>
<Image Height="Auto" Stretch="UniformToFill" x:Name="imgtmp" Visibility="Collapsed"/>
</Grid>
</Border>
<Image Height="13" Width="13" Source="../Images/rotate_small.JPG" Stretch="None" x:Name="imgRotate" Grid.Row="0" Grid.Column="3" Visibility="Collapsed"/>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="GridBox" Grid.ColumnSpan="3" Margin="0,0,2,0" Width="32" HorizontalAlignment="Left">
<Image Height="13" Width="13" Source="../Images/delete_small.JPG" Stretch="None" x:Name="imgDelete" Visibility="Collapsed" />
</StackPanel>
<Image Source="../Images/resize_small.JPG" Stretch="None" x:Name="imgResize" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Visibility="Collapsed" Opacity="0"/>
</Grid>
<TextBlock x:Name="txtLabel" Height="100" HorizontalAlignment="Left" Margin="-80,0,0,-20" Width="80" Text="X: 0 --- Y: 0" Foreground="Red" TextWrapping="Wrap" Visibility="Collapsed"/>
</Grid>
</Border>