Posts

Showing posts with the label WPF

WPF: How to pass WindowHandle to ViewModel using MVVM pattern

Create a Behavior with dependency property as shown below: using System; using System.Diagnostics.CodeAnalysis; using System.Windows; using System.Windows.Interactivity; using System.Windows.Interop; namespace My.Windows.Controls.Behaviors {     [ExcludeFromCodeCoverage] public class WindowHandleMVVMBehavior : Behavior<Window> { public IntPtr WindowHandle { get { return (IntPtr)GetValue(WindowHandleProperty); } set { SetValue(WindowHandleProperty, value); } } public static readonly DependencyProperty WindowHandleProperty = DependencyProperty.Register("WindowHandle", typeof(IntPtr), typeof(WindowHandleMVVMBehavior),  new FrameworkPropertyMetadata(IntPtr.Zero, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); protected override void OnAttached() { base.OnAttached(); if (AssociatedObject != null) { AssociatedObject.Loaded += OnWindowLoaded; } } private void OnWindowLoaded(object sender, RoutedEven...

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

Note: If your WPF UserControl is hosted in MFC Window, you may want to clean-up the ViewModel that is DataContext of your UserControl. Since Unloaded event is not called reliably for the UserControl when the hosting MFC Window closes, there is a need for another solution. XAML: < UserControl  x : Class = "Views.MyUserControl"               xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"               xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"               xmlns : mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"                 xmlns : d = "http://schemas.microsoft.com/expression/blend/2008"           ...

WPF InputBindings KeyBinding Command Example .Net 4.0

Image
WPF starting from .Net Framework 4.0 allows developers to bind commands directly to the KeyBindings as shown in the example below: A complete solution is posted here on my Google Drive:  https://docs.google.com/file/d/0Bw72YL7u1BpmaFdsSng1N3Rqbnc/edit?usp=sharing < Window  x : Class = "WPF_InputBindings_Keybinding.MainWindow"          xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"          Title = "WPF InputBindings KeyBindings Example"  Height = "400"  Width = "400"  Focusable = "True" >      < Window.InputBindings >          < KeyBinding  Key = "C"  Modifiers = "Control"  Command ="{ Binding  CopyCommand } "  />  ...

WPF: How to Style Horizontal GridSplitter

Image
< Window  x : Class = "HorizontalGridSplitterExample"          xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"          Title = "HorizontalGridSplitter Example"  Height = "300"  Width = "300" >      < Window.Resources >                   < Style  x : Key = "GridSplitterHorizontalGripStyle"  TargetType ="{ x : Type  GridSplitter } " >              < Setter  Property = "HorizontalAlignment"  Value = "Stretch"  />              < Setter  Property = "FocusVisualStyle"  V...

WPF: How to Style Vertical GridSplitter

Image
< Window  x : Class = "VerticalGridSplitterExample"          xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"  Title = "VerticalGridSplitter Example"          Height = "300"  Width = "300" >      < Window.Resources >                   < Style  x : Key = "GridSplitterVerticalGripStyle"  TargetType ="{ x : Type  GridSplitter } " >              < Setter  Property = "VerticalAlignment"  Value = "Stretch"  />              < Setter  Property = "FocusVisualStyle"  Value ="{ x :...

WPF Drag & Drop: How to detect when drag is started

Image
This article will explain how to detect when drag is started in WPF Applications. Features: 1) Reliably detect drag & drop 2) Don't confuse double clicks as drag start 3) Don't confuse scrolling as drag event  Notes: - WPF is very powerful, but one thing i find missing is an event telling developers when drag is started - The solution here involves handling Left Button Down, Mouse Move & Left Button Up event combinations to detect when drag & drop is started and when it ended Important: Even though the code posted here uses code-behind, I highly recommend using MVVM way of handling drag & drop using attached properties on controls to detect drag & drop, show visual feedback and transfer data. (I will post a WPF MVVM Drag & Drop Framework soon) Complete Visual Studio 2010 Solution https://docs.google.com/file/d/0Bw72YL7u1BpmQkl3Q3BleDgzR1E/edit?usp=sharing XAML: < Window  x : Class = "DragAndDropDragStart.MainWind...

WPF: Scroll content of control when drag & drop is in progress

It's nice to be able to scroll through content of a control when items are being dragged over a WPF control.  I have created an attached property called  "IsScrollOnDragOverEnabled", which you can set on a control with ScrollViewer as shown below to scroll the content of the control when items are being dragged over the control. Features: 1) Horizontal Scroll 2) Vertical Scroll 3) Smooth Scroll (Note: This feature scrolls content of the control in a smooth scroll fashion, to let the users see and interact with content being scrolled. Here is how to use it: < Window  x : Class = "DragAndDropFramework.MainWindow"  xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml"          xmlns : DragAndDropBehaviors = "clr-namespace:DragAndDropBehaviors;assembly=DragAndDrop"  Title = "Drag...