Wpf Reactive Extensions react when mouse is not over two elements - wpf

I'm trying to learn more about reactive extensions but I find it quite hard to find a real world example so i can train myself.
I few days ago i found myself writing some ToggleButton Mouse Enter, Leave Checked Unchecked events, and now i am wondering if i could simplify it using reactive extensions.
Here is the goal:
Given a ToggleButton, when hovering over and it's not checked, a popup should show, the popup should close if the mouse is not over the button or the popup
If I press the toggle button (Checked) the popup should stay open until the button is unchecked (ignoring mouse enter leave events) after which the mouse hover behavior should kick in again.
And if the Popup is closed externaly the toggle button should be automatically unchecked. (I know that this could be implemented using a few bindings and data triggers but i want to exercise my reactive extensions logic)
Right now i have the following:
private void ToggleButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!ToggleButton.IsChecked ?? false)
Popup.IsOpen = true;
}
private void ToggleButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!Popup.Child.IsMouseOver && !(TaskManagerTab.IsChecked ?? false))
{
Popup.IsOpen = false;
return;
}
popup.Child.MouseLeave += Popup_MouseLeave;
}
void Popup_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
Popup.Child.MouseLeave -= Popup_MouseLeave;
if (!ToggleButton.IsMouseOver && !(ToggleButton.IsChecked ?? false))
{
Popup.IsOpen = false;
return;
}
}
private void ToggleButton_CheckedChanged(object sender, System.Windows.RoutedEventArgs e)
{
Popup.IsOpen = ToggleButton.IsChecked ?? false;
if (Popup.IsOpen)
Popup.Closed += Popup_Closed;
}
void Popup_Closed(object sender, System.EventArgs e)
{
Popup.Closed -= Popup_Closed;
ToggleButton.IsChecked = false;
}
}
I would a rough version but i really don't know how to begin.
Thank you!
Update:
I did come up with this but I'm not sure about performance, and i can't seem to repeat if the toggle button is unchecked.
var mouseEnterSaveBtn = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => SaveBtn.MouseEnter += h,
h => SaveBtn.MouseEnter -= h);
var mouseLeaveList = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => popup.MouseLeave += h,
h => popup.MouseLeave -= h);
var toggleBtnChecked =
Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>(h => SaveBtn.Checked += h,
h => SaveBtn.Checked -= h);
var allCloseEvents = mouseLeaveList.Merge(mouseLeaveList);
mouseEnterSaveBtn.TakeUntil(toggleBtnChecked).Subscribe(pattern =>
{
popup.Visibility = Visibility.Visible;
allCloseEvents.TakeUntil(toggleBtnChecked).
Subscribe(eventPattern =>
{
if (!popup.IsMouseOver && !popup.IsMouseOver)
popup.Visibility = Visibility.Collapsed;
});
});

That's pretty close to what I would do. One thing I would suggest, though, is that anytime you feel like you should subscribe to an observable within an observer (notice your nested Subscribe calls), you can probably combine those observables and then subscribe.
var mouseIn = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(
h => SaveBtn.MouseEnter += h,
h => SaveBtn.MouseEnter -= h
);
var mouseOut = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(
h => popup.MouseLeave += h,
h => popup.MouseLeave -= h
);
var isMouseOver = mouseIn
.Select((o,e) => true)
.Merge(mouseOut
.Select((o,e) => false));
var toggleBtnChecked = Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>
(
h => SaveBtn.Checked += h,
h => SaveBtn.Checked -= h
)
.Select((o,e) => (o as ToggleButton).Checked);
var shouldShow = isMouseOver
.DistinctUntilChanged()
.CombineLatest(toggleBtnChecked, (mouse, pressed) => pressed || mouse);
shouldShow.Subscribe
(
shouldBeVisible =>
{
popup.Visibility = shouldBeVisible ? Visibility.Visible : Visibility.Collapsed;
}
);

Related

WPF Added button click not firing

I have a grid of custom buttons with 1 column. When i click on one it creates a new row, with its own custom buttons (depending on which was clicked). When i click on those new button it's supposed to continue but the click event doesn't even fire. It only works on the 1rst column
First set of button :
public ModificationPage(List<Tools.Modele> lm)
{
InitializeComponent();
int index = 0;
lm.ForEach(model =>
{
Console.WriteLine(model.Libelle);
ModifButton b = new ModifButton(model,0,index);
Grid.SetColumn(b,0);
Grid.SetRow(b,index);
b.Click += ButtonClicked;
grid.Children.Add(b);
index++;
});
}
private void ButtonClicked(object sender, RoutedEventArgs e)
{
e.Handled = true; //Without this the event is fired multiples times and add more than 1 row. Removing it doesn't fix the problem
ModifButton b = sender as ModifButton;
Console.WriteLine(b.model);
AddFollowing(b)
}
private async void AddFollowing(ModifButton b)
{
List<List<String>> rep = await SQL.Procedure(Constant.RecupModel, sp);
grid.ColumnDefinitions.Add(new ColumnDefinition());
int index = 0;
rep.ForEach(item =>
{
Modele m = new Modele(int.Parse(item[0]), item[1], item[4]);
ModifButton mb = new ModifButton(m, grid.ColumnDefinitions.Count - 1,index);
b.Click += ButtonClicked; //This one doesn't work
Grid.SetColumn(mb, grid.ColumnDefinitions.Count - 1);
Grid.SetRow(mb, index);
grid.Children.Add(mb);
index++;
});

Flicker on DragMove of Border-less WPF Custom Window

I have a custom, border-less window intended to act as a base form for future WPF projects. I've managed to simulate near all of the native window behaviours, but I'm having a problem with DragMove. I wanted the window to reposition itself above the task bar when the user drags the its title bar behind the windows task bar, so that the whole window is visible. I managed to do this by overriding the OnLocationChanged method, because all the mouse related handlers are blocked during DragMove.
This works like a charm, except I get a flicker when dragging the title bar along the windows task bar. I can see it is trying to redraw the window where it would have been, down below, and then it immediately redraws the window again in the intended place.
How can I interrupt this redrawing properly? Many Thanks!
This, this, this and this SO question did not help me at all, unfortunately. Here is an extract of my code:
protected override void OnLocationChanged(EventArgs e)
{
base.OnLocationChanged(e);
var activeScreenHeight = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(this).Handle).WorkingArea.Bottom - ToolbarSnapThreshold;
if (activeScreenHeight < this.RestoreBounds.Top)
{
this.Top = activeScreenHeight - this.RestoreBounds.Height + ToolbarSnapThreshold;
}
}
protected void MaximizeOrRestore()
{
this.WindowState = (this.WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
this.lastNonMinimizedState = this.WindowState;
}
protected void RestoreAndDragMove(MouseButtonState leftMouseButtonState)
{
if (leftMouseButtonState == MouseButtonState.Pressed)
{
if (this.WindowState == WindowState.Maximized)
{
var activeScreenWidth = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(this).Handle).Bounds.Width;
var windowCurrentWidth = this.RestoreBounds.Width;
var windowPositionAdjustX = this.relativeMousePositionX - (windowCurrentWidth / 2);
if (windowPositionAdjustX < 0)
{
windowPositionAdjustX = 0;
}
else if (windowPositionAdjustX + windowCurrentWidth > activeScreenWidth)
{
windowPositionAdjustX = activeScreenWidth - windowCurrentWidth;
}
this.WindowState = WindowState.Normal;
this.lastNonMinimizedState = this.WindowState;
this.Left = windowPositionAdjustX - this.relativeMousePositionX + this.originalMousePositionX;
this.Top = 0 - this.relativeMousePositionY + this.originalMousePositionY;
}
this.DragMove();
}
}
private void Window_TitleBarClicked(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.relativeMousePositionX = e.GetPosition(this).X;
this.relativeMousePositionY = e.GetPosition(this).Y;
this.originalMousePositionX = System.Windows.Forms.Cursor.Position.X;
this.originalMousePositionY = System.Windows.Forms.Cursor.Position.Y;
if (e.ClickCount == 2)
{
this.MaximizeOrRestore();
}
}
}

Proper disposal of RX subscriptions

I'm new to Reactive Extensions, and this is what I've come up with to do a Popup Toaster Notification. When the mouse goes over the toaster, the opacity is brought back to 100%. Otherwise, it fade out gradually.
The code works, but I'm not entirely confident I'm not leaking resources, especially in the mouseOut subscription. In addition, I'm not sure if this is the best way to implement this functionality.
Any critiques, tips would be appreciated.
private void rxPop()
{
Rectangle toaster = (Rectangle)this.FindName("toaster1");
Thickness newToasterPosition = new Thickness(
toaster.Margin.Left, toaster.Margin.Top,
toaster.Margin.Right, toaster.Margin.Bottom + toaster.Height);
/* Animations */
Storyboard slideInAnimation = slide(toaster,
newToasterPosition,
TimeSpan.FromMilliseconds(450));
Storyboard fadeInAnimation = animateOpacity(toaster, 1.0, TimeSpan.FromMilliseconds(150));
Storyboard fadeOutAnimation = animateOpacity(toaster, 0.0, TimeSpan.FromSeconds(3));
/* Events */
var mouseEnter = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseEnter += h,
h => toaster.MouseEnter -= h);
var mouseOut = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseLeave += h,
h => toaster.MouseLeave -= h);
var slideInCompleted = Observable.FromEventPattern<EventHandler, EventArgs>(
h => slideInAnimation.Completed += h,
h => slideInAnimation.Completed -= h);
var fadeOutCompleted = Observable.FromEventPattern<EventHandler, EventArgs>(
h => fadeOutAnimation.Completed += h,
h => fadeOutAnimation.Completed -= h);
// slideIn then fadeOut
slideInCompleted.Subscribe(e => fadeOutAnimation.Begin());
var mouseEnterSubscription = mouseEnter
.ObserveOnDispatcher()
.Do(a =>
{
fadeOutAnimation.Pause();
fadeInAnimation.Begin();
slideInAnimation.Pause();
mouseOut.Do(
b =>
{
fadeOutAnimation.Begin();
fadeInAnimation.Stop();
slideInAnimation.Resume();
}).Subscribe();
})
.Subscribe();
fadeOutCompleted.Subscribe((e) => mouseEnterSubscription.Dispose());
slideInAnimation.Begin();
}
Ideally, I would have liked to express the events in the following manner:
slideIn then fadeOut
unless mouseEnter
then fadeIn , slideIn.Pause
until mouseLeave
then fadeOut.Begin and slideIn.Resume
What's the closest way to do this in RX?
UPDATE #1
*UPDATE #2* (Cleaning up Subscribe())
Here's a somewhat cleanear attempt.
protected CompositeDisposable _disposables = new CompositeDisposable();
private void rxPop()
{
IDisposable mouseEnterSubscription = null;
/* Business logic: slideIn then fadeOut then remove from visual tree */
_disposables.Add(
slideInAnimation
.BeginUntilDone()
.Select(slideInCompletedEvent =>
fadeOutAnimation.BeginUntilDone())
.Switch()
.Subscribe(fadeOutAnimationCompletedEvent =>
{
mouseEnterSubscription.Dispose();
// remove from visual tree
(toaster.Parent as Panel).Children.Remove(toaster);
}));
/* Business logic: mouseEnter/mouseLeave should pause/restart animations */
mouseEnterSubscription = mouseEnter
.ObserveOnDispatcher()
.Do(mouseEnterEventArgs =>
{
fadeOutAnimation.Pause();
fadeInAnimation.Begin();
slideInAnimation.Pause();
})
.Select(e => mouseOut)
.Switch()
.Do(mouseLeaveEventArgs =>
{
fadeOutAnimation.Begin();
fadeInAnimation.Stop();
slideInAnimation.Resume();
})
.Subscribe();
}
public static class RxExtensions
{
public static IObservable<EventPattern<EventArgs>> BeginUntilDone(this Storyboard sb)
{
var tmp = Observable.FromEventPattern(
h => sb.Completed += h,
h => sb.Completed -= h);
sb.Begin();
return tmp;
}
}
My questions are:
Is ObserveOnDispatcher() done correctly?
Does the Switch() dispose the previous IObservable for me?
I struggle to translate the above into LINQ query syntax
/* Business Logic */
var showToast =
// Start by sliding in
from slideInComplete in slideIn.BeginObservable()
where slideInComplete
// Then in parallel, fadeOut as well as wait for mouseEnter
from fadeOutComplete in fadeOut.BeginObservable()
from enter in mouseEnter
// ... I'm lost here.
// ... how do I express
// ..... mouseEnter should pause fadeOut?
select new Unit();
Well, first off, you're leaking IDisposables all over the place - each one of those Subscribe calls returns an IDisposable, which are just going out of scope, not being disposed properly. That part is easily fixed, however, using some of the IDisposable containers in the Rx lib:
(snippets from the test harness I threw together around your example code)
// new fields
// A serial disposable lets you wrap one disposable
// such that changing the wrapped disposable autocalls
// Dispose on the previous disposable
protected SerialDisposable _resubscriber;
// A composite disposable lets you track/dispose a whole
// bunch of disposables at once
protected CompositeDisposable _disposables;
// no real need to do this here, but might as well
protected void InitializeComponent()
{
_disposables = new CompositeDisposable();
_resubscriber = new SerialDisposable();
// misc
this.Unloaded += (o,e) =>
{
if(_disposables != null) _disposables.Dispose();
if(_resubscriber != null) _resubscriber.Dispose();
};
Then later on in your queries, wrap all Subscribe calls (except one, see below) in something like this:
// slideIn then fadeOut
_disposables.Add(slideInCompleted.Subscribe(e => fadeOutAnimation.Begin()));
The one exception being the MouseOut "canceller":
.Do(a =>
{
fadeOutAnimation.Pause();
fadeInAnimation.Begin();
slideInAnimation.Pause();
_resubscriber.Disposable = mouseOut.Do(
b =>
{
fadeOutAnimation.Begin();
fadeInAnimation.Stop();
slideInAnimation.Resume();
}).Subscribe();
})
Now...as for this:
slideIn then fadeOut
unless mouseEnter
then fadeIn , slideIn.Pause
until mouseLeave
then fadeOut.Begin and slideIn.Resume
That I'll have to think about...I think there's a more...rx way of doing it, but I'd have to ponder it a bit. Definitely handle the IDisposable cleanup, tho!
(will edit this if I can come up with something for the second bit)
EDIT: Ooh, think I've got something promising...
First off, let's rig up a way to translate Storyboard start/completes into an IObservable:
public static class Ext
{
public static IObservable<bool> BeginObservable(this Storyboard animation)
{
var sub = new BehaviorSubject<bool>(false);
var onComplete = Observable.FromEventPattern<EventHandler, EventArgs>(
h => animation.Completed += h,
h => animation.Completed -= h);
IDisposable subscription = null;
subscription = onComplete.Subscribe(e =>
{
Console.WriteLine("Animation {0} complete!", animation.Name);
sub.OnNext(true);
if(subscription != null)
subscription.Dispose();
});
Console.WriteLine("Starting animation {0}...", animation.Name);
animation.Begin();
return sub;
}
}
Basically, this sets up a "Start Animation, and signal us true when it's done" sequence...on to the good part!
So let's assume you've got the following Storyboards defined:
fadeIn: Animates Opacity to 1.0
fadeOut: Animates Opacity to 1.0
slideIn: Animates Margin to "in" value
slideOut: Animates Margin to "out" value
And these observables (slight name tweaks from your code):
mouseEnter: => MouseEnter event
mouseOut: => MouseLeave event
You can set up an IObservable that carries the actual sequence thusly:
var showToast =
// Start this mess on a "mouse enter" event
from enter in mouseEnter
// Start (in parallel) and wait until the fadeIn/slideIn complete
from fadeInComplete in fadeIn.BeginObservable()
from slideInComplete in slideIn.BeginObservable()
where fadeInComplete && slideInComplete
// Until you see a "mouse out" event
from exit in mouseOut
// Then start (in parallel) and wait until the fadeOut/slideOut complete
from fadeOutComplete in fadeOut.BeginObservable()
from slideOutComplete in slideOut.BeginObservable()
where fadeOutComplete && slideOutComplete
// And finally signal that this sequence is done
// (we gotta select something, but we don't care what,
// so we'll select the equivalent of "nothing" in Rx speak)
select new Unit();
EDIT EDIT: Here's the full Test rig I used, maybe you can port to your needs:
void Main()
{
var window = new Window();
var control = new MyControl();
window.Content = control;
window.Show();
}
public class MyControl : UserControl
{
protected DockPanel _root;
protected Rectangle _toaster;
protected CompositeDisposable _disposables;
protected Thickness _defaultToasterPosition = new Thickness(10, -60, 10, 10);
public MyControl()
{
this.InitializeComponent();
}
protected void InitializeComponent()
{
_disposables = new CompositeDisposable();
_root = new DockPanel();
_toaster = new Rectangle();
_toaster.SetValue(Rectangle.NameProperty, "toaster1");
_toaster.Fill = Brushes.Red;
_toaster.Stroke = Brushes.Black;
_toaster.StrokeThickness = 3;
_toaster.Width = 50;
_toaster.Height = 50;
_toaster.Opacity = 0.1;
DockPanel.SetDock(_toaster, Dock.Bottom);
_toaster.Margin = _defaultToasterPosition;
rxPop();
_root.Children.Add(_toaster);
this.Content = _root;
this.Unloaded += (o,e) =>
{
if(_disposables != null) _disposables.Dispose();
};
}
private void rxPop()
{
var toaster = (Rectangle)this.FindName("toaster1") ?? _toaster;
var defaultToasterPosition = toaster.Margin;
var newToasterPosition = new Thickness(
defaultToasterPosition.Left, defaultToasterPosition.Top,
defaultToasterPosition.Right, defaultToasterPosition.Bottom + toaster.Height);
/* Animations */
var slideIn = slide(toaster, newToasterPosition, TimeSpan.FromMilliseconds(450));
var slideOut = slide(toaster, defaultToasterPosition, TimeSpan.FromMilliseconds(450));
var fadeIn = animateOpacity(toaster, 1.0, TimeSpan.FromMilliseconds(150));
var fadeOut = animateOpacity(toaster, 0.1, TimeSpan.FromSeconds(3));
/* Events */
var mouseEnter = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseEnter += h,
h => toaster.MouseEnter -= h);
var mouseOut = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
(h => toaster.MouseLeave += h,
h => toaster.MouseLeave -= h);
var showToast =
// Start this mess on a "mouse enter" event
from enter in mouseEnter
// Start (in parallel) and wait until the fadeIn/slideIn complete
from fadeInComplete in fadeIn.BeginObservable()
from slideInComplete in slideIn.BeginObservable()
where fadeInComplete && slideInComplete
// Until you see a "mouse out" event
from exit in mouseOut
// Then start (in parallel) and wait until the fadeOut/slideOut complete
from fadeOutComplete in fadeOut.BeginObservable()
from slideOutComplete in slideOut.BeginObservable()
where fadeOutComplete && slideOutComplete
// And finally signal that this sequence is done
// (we gotta select something, but we don't care what,
// so we'll select the equivalent of "nothing" in Rx speak)
select new Unit();
_disposables.Add(showToast.Subscribe());
}
private Storyboard slide(Rectangle rect, Thickness newPosition, TimeSpan inTime)
{
var sb = new Storyboard();
sb.Duration = inTime;
Storyboard.SetTarget(sb, rect);
Storyboard.SetTargetProperty(sb, new PropertyPath(Rectangle.MarginProperty));
var path = new ThicknessAnimation(newPosition, inTime);
sb.Children.Add(path);
return sb;
}
private Storyboard animateOpacity(Rectangle rect, double newOpacity, TimeSpan inTime)
{
var sb = new Storyboard();
sb.Duration = inTime;
Storyboard.SetTarget(sb, rect);
Storyboard.SetTargetProperty(sb, new PropertyPath(Rectangle.OpacityProperty));
var path = new DoubleAnimation(newOpacity, inTime);
sb.Children.Add(path);
return sb;
}
}
public static class Ext
{
public static IObservable<bool> BeginObservable(this Storyboard animation)
{
var sub = new BehaviorSubject<bool>(false);
var onComplete = Observable.FromEventPattern<EventHandler, EventArgs>(
h => animation.Completed += h,
h => animation.Completed -= h);
IDisposable subscription = null;
subscription = onComplete.Subscribe(e =>
{
Console.WriteLine("Animation {0} complete!", animation.Name);
sub.OnNext(true);
if(subscription != null)
subscription.Dispose();
});
Console.WriteLine("Starting animation {0}...", animation.Name);
animation.Begin();
return sub;
}
}

DataGrid select last cell programmatically

When the DataGrid below gets the focus for the first time and only the first time (ie, after some other control has had the focus), the last row, 2nd column should be focused and in edit.
I added a handler for the DataGrid.GotFocus, but it's complicated code and not getting the result above.
Anyone got an elegant, bullet proof solution?
I made tiny modifications to the code
the sender should always be the grid I want, so I just used that instead of relying on a name
When the SelectionUnit is FullRow, as my grid was before I changed it to CellOrRowHeader you
apparently can't call SelectedCells.Clear()
Code below:
private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var dg = sender as DataGrid;
if (_hasHadInitialFocus) return;
var rowIndex = dg.Items.Count - 2;
if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
{
var column = dg.Columns[dg.Columns.Count - 1];
var item = dg.Items[rowIndex];
var dataGridCellInfo = new DataGridCellInfo(item, column);
if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
dg.SelectedCells.Clear();
dg.SelectedCells.Add(dataGridCellInfo);
}
else {
var row = dg.GetRow(rowIndex);
row.IsSelected = true;
}
dg.CurrentCell = dataGridCellInfo;
dg.BeginEdit();
}
_hasHadInitialFocus = true;
}
New Question
I want to repeat the selection when the focus goes to another control in the window and then back to the grid.
I thought I could turn that _hasHadInitialFocus latch to false in a LostFocus event, but the code below is firing on cell changes.
Do you know how I should be trapping the lost focus event better, and do you agree that is the place to turn the latch off?
private void DataGridLostFocus(object sender, RoutedEventArgs e) {
_hasHadInitialFocus = false;
}
You may have to fiddle with the offsets depending on whether there's an new item row visible or not, but this works for me.
private bool _hasHadInitialFocus;
private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}
_hasHadInitialFocus = true;
}
}
I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:
private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
EditCell();
}
private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
{
if (!_hasHadInitialFocus)
{
e.Handled = true;
EditCell();
}
}
private void EditCell()
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}
_hasHadInitialFocus = true;
}
}

Silverlight Mouse Events: MouseEnter and MouseLeave conflicts

I have a collection of buttons in a grid. For each one of these buttons, I want to handle the MouseEnter and MouseLeave events to animate the height of the button (and do some other interesting stuff). It all works good until I start moving my mouse too fast over and off the buttons which eventually cause the events to take place at before the other is complete. What's the best way of making sure the events wait for eachother before being triggered?
UPDATE:
Going by x0r's advice, I refactored my code into an internal class which inherits from Button and has the required methods to perform the animations. Unfortunately, this did not really solve the problem because - I think - I'm handling the Completed event of the first animation in two separate places. (correct me if I'm wrong). Here's my code:
internal class MockButton : Button
{
#region Fields
private Storyboard _mouseEnterStoryBoard;
private Storyboard _mouseLeaveStoryBoard;
private Double _width;
#endregion
#region Properties
internal Int32 Index { get; private set; }
#endregion
#region Ctors
internal MockButton(Int32 index) : this(index, 200)
{
}
internal MockButton(Int32 index, Double width)
{
this.Index = index;
this._width = width;
}
#endregion
#region Event Handlers
internal void OnMouseEnter(Action action, Double targetAnimationHeight)
{
if (_mouseEnterStoryBoard == null)
{
_mouseEnterStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.From = 10;
heightAnimation.To = targetAnimationHeight;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseEnterStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseEnterStoryBoard.Children.Add(heightAnimation);
}
_mouseEnterStoryBoard.Completed += (s, e) =>
{
action.Invoke();
};
_mouseEnterStoryBoard.Begin();
}
internal void OnMouseLeave()
{
if (_mouseLeaveStoryBoard == null)
{
_mouseLeaveStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = 10;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseLeaveStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseLeaveStoryBoard.Children.Add(heightAnimation);
}
if (_mouseEnterStoryBoard.GetCurrentState() != ClockState.Stopped)
{
_mouseEnterStoryBoard.Completed += (s, e) =>
{
_mouseLeaveStoryBoard.Begin();
};
}
else
{
_mouseLeaveStoryBoard.Begin();
}
}
#endregion
}
UPDATE 2:
Some events are getting triggered multiple times. An example of that is the Click event on the close button of my Rule object...
public Rule(Action<Int32> closeAction)
{
this.Style = Application.Current.Resources["RuleDefaultStyle"] as Style;
this.CloseAction = closeAction;
this.Loaded += (s, e) =>
{
if (_closeButton != null)
{
_closeButton.Click += (btn, args) =>
{
if (this.CloseAction != null)
{
this.CloseAction.Invoke(this.Index);
}
};
if (_closeButtonShouldBeVisible)
{
_closeButton.Visibility = System.Windows.Visibility.Visible;
}
}
};
}
And below is the Action<Int32> I'm passing to the Rule object as the CloseAction:
private void RemoveRule(Int32 ruleIndex)
{
Rule ruleToRemove = Rules.FirstOrDefault(x => x.Index.Equals(ruleIndex));
Storyboard sb = new Storyboard();
DoubleAnimation animation = new DoubleAnimation();
sb.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Opacity"));
animation.Duration = TimeSpan.FromMilliseconds(300);
animation.From = 1;
animation.To = 0;
sb.Children.Add(animation);
Storyboard.SetTarget(animation, ruleToRemove);
sb.Completed += (s, e) =>
{
if (Rules.FirstOrDefault(x => x.Index.Equals(ruleIndex)) != null)
{
this.Rules.RemoveAt(ruleIndex);
}
};
sb.Begin();
}
UPDATE 3:
In order to avoid the animations running too early, I thought I could delay the MouseEnter event, so if the user just scrolls over the item too fast, it doesn't kick off. But I have a problem now: Say the user mouses over the item and then mouses out. If I use the Storyboard.BeginTime property, that won't safe guard against that behavior because eventhough the animation gets delayed, it's still going to start eventually... So is there a way I could prevent that from happening?
Any suggestions?
check in your mouseleave eventhandler if the first storyboard is still running and if that is the case attach the starting of the second storyboard to the Completed event of the first storybaord:
private void OnOddRowMouseLeave(object sender, MouseEventArgs e)
{
...
if(_firstStoryboard.GetCurrentState() != ClockState.Stopped)
_firstStoryboard.Completed += (s,e) => _secondStoryboard.Begin();
else
_secondStoryboard.Begin()
Everything that Silverlight does is asyncronous and so most likely what is happening is that because you are moving quickly in and out of the box the mouse leave is being fired before the mouseenter has a chance to finish. You could setup your two events so thay they have an indicator of whether or not the other is in process. For example you could do this
bool mouseOut =false;
bool mouseIn =false;
void OnMouseEnter(Action action, Double targetAnimationHeight)
{
if(!this.mouseOut)
{
this.mouseIn = true;
if (_mouseEnterStoryBoard == null)
{
_mouseEnterStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.From = 10;
heightAnimation.To = targetAnimationHeight;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseEnterStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseEnterStoryBoard.Children.Add(heightAnimation);
}
_mouseEnterStoryBoard.Completed += (s, e) =>
{
action.Invoke();
};
_mouseEnterStoryBoard.Begin();
if(this.mouseOut)
{
this.OnMouseLeave();
}
this.mouseIn = false;
}
}
void OnMouseLeave()
{
if(!this.mouseIn)
{
this.mouseOut = false;
if (_mouseLeaveStoryBoard == null)
{
_mouseLeaveStoryBoard = new Storyboard();
DoubleAnimation heightAnimation = new DoubleAnimation();
heightAnimation.To = 10;
heightAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
_mouseLeaveStoryBoard.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
Storyboard.SetTarget(heightAnimation, this);
_mouseLeaveStoryBoard.Children.Add(heightAnimation);
}
if (_mouseEnterStoryBoard.GetCurrentState() != ClockState.Stopped)
{
_mouseEnterStoryBoard.Completed += (s, e) =>
{
_mouseLeaveStoryBoard.Begin();
};
}
else
{
_mouseLeaveStoryBoard.Begin();
}
}
else
{
this.mouseOut = true;
}
}
I haven't actually checked this code but this should help you to at least get closer to what you want. This should be quick enough that your user doesn't realize that it is not firing exactly on exit if they go over it quickly. But this should help to keep you from getting overlap.
Another way you could do this is setup the initial events as null, and have the mouse in event set the mouse in event when it is complete but the problem with that is that if the mouse out fires before the event is set then you don't event get the event firing.

Resources