WELCOME

This blog is where I post about my consulting work with Microsoft Technologies, and other random tidbits that don't fit in my Photo Blog or my Iraq Blog.

Thursday, June 18, 2009

Happy Early Father's Day To Me!


300_9227, originally uploaded by ScrappyDog.

The kids all got matching shirts (including one for me) for a Father's Day picture...

Thursday, June 11, 2009

MVVM Questions: Can a View Model close a window and other puzzlers

Many sample WPF applications written using MVVM (Model - View - View Model) only use a single Window, but the LOB (Line Of Business) applications I’ve been working on use LOTS of windows for different business tasks, and this has brought up a number of interesting questions about the “right” or “best” or “most practical” way to manage multiple Windows in a WPF application using MVVM?

Questions:

  1. Does a Window create it’s View Model?
  2. Or does the View Model create it’s Window?
  3. Or does “something else” create the View Model and the Window and then bind them together?
  4. How do you close a Window? 
  5. Can a View Model close itself and it’s Window?

Our Answers:

  1. We created a “Window Service” which is basically a Singleton with a Dictionary of all the open Windows and View Models in the application, and a few related methods to deal with opening and closing Windows .
  2. To open a Window you supply the Window Service with Window and a View Model.  The Window Service deals with binding the two together, showing  the Window, and then keeping track of the open Windows.
  3. If a user (or something else) tries to close a Window we catch the Window_Closing event  using AttachedCommandBehavior (see XAML below) and call a command on the View Model Base Class to give the View Model control over the closing event.
    <acb:CommandBehaviorCollection.Behaviors>
    <acb:BehaviorBinding Event="Initialized" Command="{Binding Path=Window_InitializedCommand}" />
    <acb:BehaviorBinding Event="Activated" Command="{Binding Path=Window_ActivatedCommand}" />
    <acb:BehaviorBinding Event="Closing" Command="{Binding Path=Window_ClosingCommand}" />
    </acb:CommandBehaviorCollection.Behaviors>



  4. View Models have the ability to close themselves (and their related Window) by calling the Window Service.

Wednesday, June 10, 2009

A simple method of binding the selected values of a list in WPF with MVVM

Using the MVVM pattern in WPF applications simplifies some aspects of developing and testing WPF applications, but it add quite a bit of complexity to others…

One interesting challenge I faced recently with MVVM was how to achieve two way binding of the selected values of a ListBox to a collection in a view model.  I would like to share an approach to solving this challenge using Styles and Databinding.

It’s easy to bind the values of a collection in a view model to a list, but the challenge is that you want the view model to be aware of user selections in the list without having to use traditional events an code behind.

You also want to be able to change the selected items in the view model and have those changes reflected in the UI. In my sample code (below) this is demonstrated by the “Select All” checkbox, which modifies the select items in the view model.

The key to this approach is that entities in the collection you are binding to have to have a boolean “IsSelected” property (you can name it whatever you like).  And then you create a Style to bind the IsSelected property of the ListBoxItem to the IsSelected property of the bound entity.  See the XAML sample code below:

<ListBox ItemsSource="{Binding PersonCollection}" 
SelectionMode="Multiple" >

<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
</Style>

<DataTemplate DataType="{x:Type MultiSelectMvvm:Person}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}"/>
<Label Content="{Binding IsSelected}"/>
</StackPanel>
</DataTemplate>
</ListBox.Resources>

</ListBox>

<CheckBox IsChecked="{Binding SelectAll}" Margin="3">
Select All
</CheckBox>


Here is the ViewModel code:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace MultiSelectMvvm
{
public class Window1ViewModel
{
ObservableCollection<Person> personCollection;
public ObservableCollection<Person> PersonCollection
{
get
{
if (personCollection == null)
{
personCollection = new ObservableCollection<Person>();
}
return personCollection;
}
set { personCollection = value; }
}

private bool selectAll;
public bool SelectAll
{
get
{
return selectAll;
}
set
{
selectAll = !selectAll;
foreach (var person in personCollection)
{
person.IsSelected = selectAll;
}
}
}
}


public class Person : INotifyPropertyChanged
{
public string Name
{
get;
set;
}

private bool? isSelected;
public bool? IsSelected
{
get
{
return isSelected;
}
set
{
isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}


FYI: Marlon over at C# Disciples wrote a recent blog post about this: Sync Multi Select Listbox with ViewModel.  His approach uses Attached Properties.

Tuesday, June 9, 2009

Microsoft paying to advertise Bing on Google?!?!

I found this hilarious on a variety of levels: Microsoft is paying Google to place ads for it's new Bing search engine on Google (see the screen grab from my Gmail inbox below).

It has got to cause some serious pain at Microsoft to pay this bill. Did they use Steve Balmer's credit card to setup the Adwords account?

Who is the person at Google that signed off on this? It takes some balls to happily place ads for your competition on your own site.