Silverlight MouseLeave showing inconistent behaviour - silverlight

So, my problem is that the MouseLeave event only gets triggered if I move my cursor at a certain speed. Below is a Thumb with a Border. Click the thumb and keep the mouse button down. Move slowly outside the border = no event, Move fast outside the border = event.
<Grid x:Name="LayoutRoot" Background="White">
<Border BorderBrush="Black" BorderThickness="3" Width="200" Height="100"
MouseLeave="Border_MouseLeave">
<Thumb />
</Border>
</Grid>
private void Border_MouseLeave(object sender, MouseEventArgs e)
{
MessageBox.Show("Border_MouseLeave");
}
Is it a bug in silverlight or am I missing something?
Thanx
/Mike

Thanx for the anwers, here's an update:
It seems to have something to do with MouseCapture as Guy suggests. The reason being that I also get the problem using Button or a Rectangle that captures the mouse in code-behind.
If I put a Grid between the Thumb and the Border the problem disappears so I think I will go with that.
I also noticed a related problem as I played around some.
<Grid Width="200" Height="100" Background="Transparent"
MouseLeave="Border_MouseLeave">
<Button />
<Rectangle Width="40" Height="40" Fill="Violet"
HorizontalAlignment="Left" />
</Grid>
If I press the left button and move the cursor to the left over the Rectangle and out the MouseLeave event is not registered.
This is all very strange.

Related

Detect mouse left button down (Click) outside a border WPF MVVM

I have a border in MVVM. What I am trying to achieve is to detect mouse left button down outside the border and then hide it. I can do it within the MouseLeftButtonDown event for main window, but I do not know if it is the best solution. How could I do this? I want to avoid this click to interfere with other events, for example, this border is placed in a stackpanel and the stackpanel is being hidden on mouse left button double click.
<Border Grid.Row="2"
x:Name="customPopup"
CornerRadius="10,10,0,0"
Height="25" Margin="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="Auto"
BorderBrush="DarkBlue"
BorderThickness="1"
Background="AntiqueWhite">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center">
<Image Source="/Common.Images;component/Images/Info.png"
Height="20"
Width="20" Stretch="Fill"/>
<TextBlock Margin="5"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Background="Transparent"
FontSize="12">
<Run Text="Click outside to close it"/>
</TextBlock>
</StackPanel>
</Border>
Using MouseDown on the window is probably your best bet to get your desired results. You'll need to do some calculations in order to see if the cursor is outside of the border. Something like:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
// get positions
Point mouseLoc = Mouse.GetPosition(null);
Point borderLoc = customPopup.TranslatePoint(new Point(0,0), null);
// check if the mouse is outside the border
if((mouseLoc.X < borderLoc.x || mouseLoc.X > (borderLoc.X + customPopup.ActualWidth)) && (mouseLoc.Y < borderLoc.Y || mouseLoc.Y > borderloc.Y + customPopup.ActualHeight))
{
// hide the border
}
}
Then to handle the double click, use PreviewMouseDoubleClick. Since Preview events are tunneling rather than bubbling, the double click should get called even if you have a single click event on the same element.
private void Window_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// double click code...
e.Handled = true;
}

Popup is not a proper child of its parent window?

I'm trying to create a stylish window with icon in the top-right corner that is partly outside the window boundary. Since this is not directly possible (content falling outside the window is clipped), I'm using a Popup control for it.
<Popup Placement="Right" HorizontalOffset="-80" VerticalOffset="-40" IsOpen="True" AllowsTransparency="True"
PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Grid}}">
<Image Source="pack://application:,,,/graphics/AdminIcon.png" Stretch="None" />
</Popup>
It works fine, as long as I don't switch between windows. Thereupon it looks like the Popup is neither a proper child of the Window, nor a fully separate window of its own; something in-between.
I'll try to explain it with two images:
Image1: Window loaded and popup showing nicely in the right-top corner:
Image2: ALT + TAB: Notepad somehow injects itself between Popup and Window
How do I solve this problem?
Instead of a Popup you may use a borderless, transparent Window:
<Window ...
WindowStyle="None" AllowsTransparency="True" Background="{x:Null}"
MouseLeftButtonDown="Window_MouseLeftButtonDown">
<Grid>
<Grid Margin="0,20,0,0" Background="White">
... window content goes here
</Grid>
<Image
Source="/graphics/AdminIcon.png" Stretch="None"
HorizontalAlignment="Right" VerticalAlignment="Top" />
</Grid>
</Window>
with this MouseLeftButtonDown handler
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
and perhaps a close button somewhere.

ScrollViewer Focus Error in WPF in Code behind

To create a circle below each time the button is clicked. In canvas
position of Circle move to out of canvas.
But, Scroll viewer does not move up and down.
<ScrollViewer x:Name="scv" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="135,0,0,0" Focusable="True">
<Canvas x:Name="canvas" HorizontalAlignment="Left" Height="373" VerticalAlignment="Top" Width="685" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True"/>
</ScrollViewer>
Is there a way to move up and down the scroll Viewer?
edit
An attempt was made to try to use the code to the link to me the site, an error occurs
don't use code... Anything simple way?

wpf ScrollViewer and SizeChanged event

Can anyone explain to me the relationship between ScrollViewer and SizeChanged event? Whenever I put a scrollViewer around a grid, numerous SizeChanged event gets fired. What is the relationship between the two? Thanks a lot.
EDIT:
From mdm20's comment, I noticed that the ActualWidth and ActualHeight of the grid increases continuously if I wrap the grid around a ScrollViewer. Can anyone explain why this is the case? Do I need to have hard values for the width and height of the grid?
EDIT #2:
The resizing is done through code posted below. Thanks for looking into this
private void chartGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
textCanvas.Width = chartGrid.ActualWidth;
textCanvas.Height = chartGrid.ActualHeight;
legendCanvas.Children.Clear();
chartCanvas.Children.RemoveRange(1, chartCanvas.Children.Count - 1);
textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);
AddChart();
}
Corresponding XAML code is below:
<ScrollViewer Name="chartScrollViewer">
<Grid Margin="0" x:Name ="chartGrid" Grid.Column="1" Grid.Row="1" ClipToBounds="True" Background="Transparent" SizeChanged="chartGrid_SizeChanged">
<Canvas Margin="2" Name="textCanvas" ClipToBounds="True" Grid.Column="1" Grid.Row="1" Height="1200">
<Canvas Name="chartCanvas" ClipToBounds="True">
<Canvas Name="legendCanvas" Background="Transparent" />
</Canvas>
</Canvas>
</Grid>
</ScrollViewer>
You are getting into a loop. I think what is happening is that when you change the canvas size, it prompts the grid to do a layout pass, which causes the ScrollViewer to do a layout pass, which causes the grid to resize itself, which starts the cycle over again.

How do I swallow the dropdown behavior inside an Expander.Header?

I would like to prevent an Expander from expanding/collapsing when users click inside the header area. This is basically the same question as Q 1396153, but I'd appreciate a more favorable answer :)
Is there a non-invasive way to do this? I am not sure exactly how to attach behavior to the Expander.Header content to prevent mouseclicks. I'm willing to float in content outside the expander itself via a fixed grid layout, but I'm not keen on the solution. Ideas?
XamlPad sample XAML:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Expander>
<Expander.Header><TextBlock>
When I click this text,
I don't want to trigger expansion/collapse! Only when I click the
expander button do I want to trigger an expand/collapse!
</TextBlock></Expander.Header>
<Grid Background="Red" Height="100" Width="100" >
</Grid>
</Expander>
</Page>
You can stop mouse clicks on the text box from being handled by your application.
XAML:
<Expander>
<Expander.Header>
<TextBlock MouseDown="TextBlock_MouseDown">
When I click this text,
I don't want to trigger expansion/collapse! Only when I click the
expander button do I want to trigger an expand/collapse!
</TextBlock>
</Expander.Header>
<Grid Background="Red" Height="100" Width="100" >
</Grid>
</Expander>
Code behind:
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
I don't know if this is an abomination, but, I've moved the content out of the Expander.Header and done some Grid/fixed layout/Panel.ZIndex trickery to make it appear that the content is in the Expander.Header...but it's not. This works, but it's horrible.

Resources