WPF
WPF
Today, I finally resolved an issue that I thought was related to WPF’s focus implementation. For some reason, I couldn’t get certain elements to get focus when I clicked on them. This was messing up a number of other things, including keyboard interactions and routed commands. I thought that I had some strange scenario that was preventing focus from going to my canvas. Maybe some issue with FocusScopes? Maybe the control wasn’t Focusable? It turns out that I had a number of event handlers set up for clicking, dragging, etc. In these, I was setting e.handled = true. Not...
A lot of the MVVM guidance around commands suggest using a generic ICommand implementation, such as the RelayCommand or GenericCommand implementations. These are great for UI pieces that are bound directly to an underlying ViewModel and I use them frequently. There are still cases where a RoutedUICommand is exactly what one needs. Take, for instance, toolbar buttons or menu items that need to act on pages that are loaded in a WPF frame. In this case, a RoutedUICommand is appropriate since it is decoupled from the page, which may or may not be loaded at the time. ...
When working with WPF resources, remember to declare the resources in the correct order. A style or template that is referenced elsewhere must be defined before it can be used. In other words, if Style1 uses BrushA as a StaticResource, then BrushA must be defined before Style1. Otherwise, you’ll get fun errors like: Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Windows.FrameworkTemplate' It may be common knowledge, but it can be a real pain to diagnose. This is especially true if you have a large set of template and styles for...
Laurent Bugnion’s work on MVVM Light introduced me to the ViewModel Locator pattern. The concept and implementation are really slick!
From my perspective, ViewModel Locators do the following:
Provide a simple, declarative way to connect a View to a ViewModel (duh).
Enable design-time support, including the ability to connect an alternate data source behind the VM (cool!).
I want a solution that lets me:
Connect my XAML views to their associated ViewModels
Handle a somewhat complex model with a hierarchy of domain model entities and...
Scenario A user selects a new value from a WPF combo box. You ask if they are sure they want to do this. User says “No”. WPF app is built with MVVM such that the combo box’s SelectedItem is bound to a property on the ViewModel. In the setter, you prompt the user and attempt to cancel the selection by discarding the new selected value. Here’s the UI XAML <Window x:Class="WpfComboBoxCancelSelect.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cancellable...
I had a need to make my WPF app remember it’s size, position and state for the main window. A bit of “Binging” and I eventually heard the sound of found. Turns out there are a couple ideas out there, but the WINDOWPLACEMENT solution from MSDN seemed to fit the bill the best. This solution requires using some WIN32 API calls to get and set the information. The upside is that it seems to leverage the smarts built in to Windows regarding multi-monitor detection. The sample says that if the app was previously displayed on a secondary monitor that...
I’ve been discussing an architectural question with a colleague this week. The scenario includes a WPF application, Domain Driven Design and MVVM. The goal is a relatively “pure” domain model where the classes in the model do not depend on any UI specific or other external assemblies. They are pretty much pure “POCO” objects.
The question is whether it’s reasonable to have the domain model implement INotifyPropertyChanged and INotifyCollectionChanged (via ObservableCollection).
INotifyPropertyChanged is defined in System.dll. It’s part of System.ComponentModel namespace.
INotifyCollectionChanged and ObservableCollection are both in WindowsBase.dll. These are in System.Collections.Specialized and System.Collections.ObjectModel respectively.
My take… go for...
For the past two days, I’ve been trying to get the infrastructure in place for skinning a WPF application. My goal is to support multiple skins and swap them out dynamically while the app is open. I could get the skin to affect the UI if I set it in the main window’s resource dictionary. However there was a quirk… The application uses WPF frames and pages. The skin was not showing up on frame’s pages. It would only show up on the main window. I searched Bing, Google and StackOverflow for answers. No luck. I...
In part 1, I asked the question “Do I expose the model directly, or do I wrap each item in its own view model?” We looked at two plausible options for handling this and saw some code examples of each. In part 2, I asked the question “Do I expose the model’s collections directly, or do I wrap each collection?” We saw the progression of code to implement this "brute-force". Then we quickly ran away screaming. This installment (part 3) asks "How do I cleanly and easily wrap the model’s collections with collections of ViewModels?" We'll take a...
In my previous post, I asked the question “Do I expose the model directly, or do I wrap each item in its own view model?” We looked at two plausible options for handling this and saw some code examples of each.
Now it’s time for a more interesting and challenging scenario: Do I expose the model’s collections directly, or do I wrap each collection? Let me paint this picture a bit so that we can see the scenario and the challenges.
MVVM says that a ViewModel sits between the View (XAML) and the Model. The ViewModel (VM) exposes data from the model...
The WPF community seems pretty happy with the MVVM pattern for WPF development. I can see why. It’s a great fit for WPF and its strong data-binding support. It provides a very nice layer for managing UI state, translating, formatting, and aggregating data.
So let’s say that I adopt MVVM for my project. I think there are some scenarios that still need definition. One of these is “To Wrap or Not to Wrap?” Let me explain…
MVVM says that a ViewModel sits between the View (XAML) and the Model. The ViewModel (VM) exposes data from the model plus additional view-specific details that...
Since WPF likes INotifyPropertyChanged on bound objects, I looked for a snippet to help out.
Matthias Shapiro had a good version for the interface implementation and a property declaration. I made a couple adjustments to the snippets based on personal preference (check if the value has changed before raising the event / remove the region from the property.
Then added some property verification support based on the snippet sample from Philipp Sumi. The property verification uses reflection to check if the property exists. If not, it throws an exception. This function is marked with a Conditional attribute so that it...