Skip navigation

When we got to yesterday’s cliffhanger, we had all we needed to allow a developer to wire up a group of checkboxes to be checked or unchecked in response to the status of a single "Check/Uncheck All" checkbox with no code-behind. But the Markup to take advantage of it was ugly…well I never showed the markup to take advantage but believe me it was ugly.

In order to save my fellow WPF developers from Carpal Tunnel Syndrome, I created a markup extension that would enable developers to create a MultiSelectionTarget using Attribute Syntax (similar to declaring a Binding and all that other fun stuff). As I hinted to earlier, creating a Markup Extension is not as scary as it sounds at first. You extend the MarkupExtension class; expose properties that your Markup Extension needs to properly do its job; and override the ProvideValue function to return what the user of your Markup Extension expects (in this case a MultiSelectionTarget). Here it is in all it’s glory:

   public class MultiSelectionTargetExtension : MarkupExtension
    {
        private String path;
        private MultiSelectionSource source;

        public String Path
        {
            get { return path; }
            set { path = value; }
        }

        public MultiSelectionSource Source
        {
            get { return source; }
            set { source = value; }
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (source != null && !String.IsNullOrEmpty(path))
            {
                return new MultiSelectionTarget(source, path);
            }
            return null;
        }
    }

See, piece of cake right. Any former Java developers who created JSP Tags before will be very comfortable with this convention. I’m sure ASP.Net offers similar functionality…but I’m not an ASP.Net developer. Anyway, all of this gives us a very straightforward, all XAML method to check a group of checkboxes at once…

<Window x:Class="SelectAllDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SelectAllDemo" 
    Title="SelectAllDemo" Height="300" Width="300"
    >
  <Window.Resources>
    <local:MultiSelectionSource x:Key="SelectionSource"/>
  </< span>Window.Resources>
  <StackPanel>
    <CheckBox IsChecked="{Binding Source={StaticResource SelectionSource}, Path=IsSelected}"
              FontWeight="Bold">Select All</< span>CheckBox>
    <CheckBox local:PowerSelector.MultiSelectorTarget=
                "{local:MultiSelectionTarget Source={StaticResource SelectionSource},
                Path=IsChecked}
">
      Check me 1
    </< span>CheckBox>
    <CheckBox local:PowerSelector.MultiSelectorTarget=
                "{local:MultiSelectionTarget Source={StaticResource SelectionSource},
                Path=IsChecked}
">
      Check me 2
    </< span>CheckBox>
  </< span>StackPanel>
</< span>Window>

So there you have it. Attached Properties, Multibindings, and MarkupExtensions have come together in harmony to enable multi selection in pure XAML. Further refactoring that I can do with the PowerSelector include making it work with comboboxes as well as checkboxes. This util is going into the WPF Toolbelt, but you can download the sample now.

Up next…using attached properties to make a standard GridView sortable.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: