Skip to main content

Posts

Showing posts from November, 2017

ContextMenu disabled at first?

ContextMenu has an issue that results in the menu being initially disabled. Regardless of whether what they are bound to is set to enabled or disabled. This will be true until a valid left click is done, usually on a button or the content it self. The root cause is that the ContextMenu is on a separate visual and logical tree than the window. This results in the items being disabled initially until a click is made first (not right click, but left click). Adding the following in the XAML to the menu item will resolve this issue: 1 <MenuItem Head... CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />

Updating UI during long operation

If you are performing an extensive operation that has multiple UI updates, for example in a loop, then you can use the following code to keep the UI updated alongside the operation. 1 2 3 4 5 6 7 8 9 10 private void UpdateUI() { DispatcherFrame update = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback( delegate ( object parameter) { update.Continue = false ; return null ; }), null ); Dispatcher.PushFrame(update); } Simply call that function inside your loop and the UI will update. Note, I have set the priority to background, you may have to set that to "Render"

ActualTop and ActualLeft after maximizing?

In wpf windows, maximizing the window can result to some funny scenarios. Window.Left and Window.Top actually don't get updated. I've looked around and a lot of people suggest using a pinvoke or reflection to get the updated values. I found that what is a little cleaner is to use the following, and it still gets the true actual  data Point ActualLeftActualTop = this .PointToScreen( new Point(0, 0));