November 2011
4 posts
6 tags
Dynamic View Model
When doing MVVM I find that much of the time putting together the view model is spent creating properties that look like the following: private string _userName; public string UserName { get { return _userName; } set { if (_userName != value) { _userName = value; RaisePropertyChanged("UserName"); } } } Especially while just banging...
Nov 25th
10 notes
7 tags
Key Input Text Box
On a recent project I had the need to allow the user to input a key stroke. After a quick Google search, I decided to roll my own. The following is the result. First the boring stuff. Three dependency properties to hold the values. public class KeyInputTextBox : TextBox { public static readonly DependencyProperty KeyProperty = DependencyProperty.Register("Key", typeof(Key),...
Nov 20th
6 notes
5 tags
RemoveWhere extension method
Often we need to iterate over a collection and remove items that meat a given criteria. Sure List<T> has RemoveAll, but what about the cases where you do not have a List<T>? Typically this results in code like the following: var collection = new ObservableCollection<int> { 7, 2, 3, 4, 6, 5 }; var toRemove = collection.Where(x => x % 2 == 0).ToList(); foreach (var item in...
Nov 10th
13 notes
7 tags
MVVM Popups
Recently I was faced with an interesting problem of how to properly display a popup and still follow the MVVM pattern. After a lot of thought, the following is the solution that I have finally landed on. First of all the main window for the application: <Window x:Class="MVVMPopup.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ...
Nov 1st
8 notes