NumericTextBoxBehavior for XAML-based Windows Store Applications

One of the first (and arguably most useful) behaviors that I wrote for MVVMHelpers was a NumericTextBoxBehavior.  This is a WPF behavior that restricts the input for a TextBox to be numeric-only.  Recently, someone emailed me asking if it was possible to do the same thing in a Windows Store application.  It absolutely is, but there are some caveats. My original implementation for WPF utilizes preview events - these are events which are sent to the ancestors of the element prior to it receiving a … [Read more...]

Default and Cancel button behaviors

One of the things I miss from WPF moving to Windows Store Apps is the ability to define a "Cancel" and "Default" button.  These are buttons which are automatically invoked when you press ENTER or ESC.  A prompt from a fellow developer got me to thinking about how we could accomplish this with an attached behavior until Microsoft decides to add the support into the framework.  Here's the usage I wanted:   A few rules: It should not invoke the default or cancel buttons if they … [Read more...]

Behaviors in Windows 8.1 Store Apps

Visual Studio 2013 RC was released to MSDN this week and it includes some very cool new features which have been detailed on various blogs around the Internet.  One feature I've not seen much coverage of, but am very excited about is support for Blend behaviors.  If you create a new Windows Store application using the 8.1 profile you will find Blend behaviors in the Assets tab: The list of behaviors is small - compared to WPF and Silverlight, but all the important ones are there, along with a … [Read more...]

Adding a watermark to a PasswordBox in a Windows Store app

In the previous post, I wrote about a Blend behavior for Windows Store apps to add a watermark to a TextBox.  The next question I got was "Well, what about a PasswordBox?" PasswordBox is a bit tricker since it doesn't allow text to be displayed in the clear - so our little trick of changing the Text property doesn't work here.  So, instead, let's get a little hacky (or clever depending on how you look at it I suppose).  We can use the same series of events (GotFocus/LostFocus/Loaded) but instead … [Read more...]

Watermark TextBox in Windows Store apps

A common request for WSAs is to add a "watermark" to TextBox entries so users get a hint as to what is expected in the TextBox. You can see this in many Search Charm implementations as it allows a search hint to be provided via the SearchPane.PlaceHolderText property.  However, the built-in TextBox in XAML doesn't have this feature (HTML does!). There are a few custom controls out there which allow for this, however, I'm a big fan of behaviors over custom controls - so when I needed this, I … [Read more...]

Synchronizing Collections in Windows Store Apps

One issue that often comes up when building WSAs is managing selection in your filled/snapped/full screen views.  The goal, of course, is to provide a reasonable view as the user snaps your application which is often done by providing two UIs within the page - one with a GridView for filled or full screen and the second with a ListView for snapped view.  You hide and show the two views using the Visual State Manager when using the normal templates. If selection is provided, then ideally it … [Read more...]

Using behaviors with ElementName bindings in Windows 8 Metro

I got an email yesterday from a developer who was trying to use MVVMHelpers and the new System.Windows.Interactivity support for Windows 8 and was having trouble. Specifically, he was trying to do this: The idea was to have a command get invoked when an item is tapped in the ListView - passing the selected item as the parameter to the command. He was always getting null in his command handler. There are actually two issues with this snippet, one is in the setup of the ListView itself and the … [Read more...]

Blend Behaviors in Styles: DragPositionBehavior

In the last post, I referred to the DragPositionBehavior in the JulMar MVVM library.  This behavior allows any UIElement to be dragged and repositioned using the mouse without requiring any code logic on your part.  It’s easy to apply – using the traditional Blend syntax (easiest done by dragging the behavior onto an element):     <Interactivity:Interaction.Behaviors>         <julmar:DragPositionBehavior /> … [Read more...]

MVVM: IUIVisualizer and event management with behaviors

In this post, we will look at the IUIVisualizer, and bring together some of the concepts we’ve talked about already through a new sample – a simple picture viewer: The application grabs all the images from the user’s photo folder and then displays each one onto the surface of a corkboard.  You can change the properties of an image, remove an image or add additional images.  It’s not designed to be a true image application ala photoSuru, it’s really more of a sample of MVVM practices … [Read more...]

Playing with WPF Behaviors – a WatermarkText behavior

One of the coolest new features of Blend 3 is the inclusion of behaviors. This new feature formalizes the "attached behavior" model that has become so prevelant in WPF (and Silverlight) development.  I won't go into details on the architecture - instead I'll refer you to a nice reference: To play with this new support, I built a WatermarkTextBehavior which places a watermark into a TextBox … [Read more...]