Click here to Skip to main content
6,594,088 members and growing! (12,391 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Data Binding     Intermediate License: The Code Project Open License (CPOL)

AttachedPropertyEvent Pattern

By Jan-Erik Romoeren, Björn Asplund

Pattern for publishing an event by using attached property and IEventAggregator
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 3.5, .NET 4.0), XAML, WPF, Silverlight, Dev
Version:7 (See All)
Posted:2 Jul 2009
Views:2,028
Bookmarked:8 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 3.03 Rating: 4.33 out of 5

1

2
1 vote, 20.0%
3

4
4 votes, 80.0%
5

Introduction

In our project we use the Composite WPF model, and we needed a way to publish events from our views (XAML code) without creating any code-behind, and without having any dependencies. To publish and subscribe to events, we already used IEventAggregator, but still we had to write some code-behind in order to publish the events.

So we came up with what we call "AttachedPropertyEvent".

About the Example

In this example, I'm going to implement SelectedItemsChanged on a datagrid. The datagrid resides in a view, and I wish to subscribe to this event in some viewmodel.

Implementation

First we create an AttachedProperty with get and set methods, and a callback to perform some logic. This class resides in our Infrastructure project, and namespace Events.

public static class SelectedItemsChangedApe
{
    public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.RegisterAttached("SelectedItems",
                                    typeof (object), 
                                    typeof (SelectedItemsChangedApe), 
                                    new PropertyMetadata(RaiseEventCallback));

    public static object GetSelectedItems(DependencyObject obj)
    {
        return obj.GetValue(SelectedItemsProperty);
    }

    public static void SetSelectedItems(DependencyObject obj, object value)
    {
        obj.SetValue(SelectedItemsProperty, value);
    }

    private static void RaiseEventCallback
	(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Implement logic here
    }
}   

Next we need to implement the attached property in our View (XAML). We're using Xceed DataGridControl as our datagrid.

<xceed:DataGridControl
    x:Name="gridControl"
    ItemsSource="Some item source"
    Events:SelectedItemsChangedApe.SelectedItems=
	"{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem}" /> 

By now we have an attached property, which really doesn't do much. What we want to do now is to publish an event every time the selected items change. To achieve this, we use IEventAggregator. So back to our Infrastructure project, we start by creating our own event, deriving from CompositePresentationEvent (Microsoft.Practices.Composite.Presentation.Events).

public class SelectedItemsChangedEvent : CompositePresentationEvent<IList<object>> { } 

So, with our composite presentation event, we can go back to our attached property and implement our logic in the callback method. What we want to do is publish the SelectedItemsChangedEvent. First we get our IEventAggregator by using the ServiceLocator, and publish the event to the event aggregator.

private static void RaiseEventCallback
	(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    IEventAggregator eventAggregator = 
	ServiceLocator.Current.GetInstance<IEventAggregator>();
    eventAggregator.GetEvent<SelectedItemsChangedEvent>().Publish
					(e.NewValue as IList<object>);
}

Now our event is published, and we just need to subscribe to it. In this example, I have some viewmodel, where I subscribe to the event within the constructor.

public SomeViewModel()
{
    IEventAggregator eventAggregator = 
	ServiceLocator.Current.GetInstance<IEventAggregator>();
    eventAggregator.GetEvent<SelectedItemsChangedEvent>().Subscribe
					(OnSelectedItemsChanged);
}

private void OnSelectedItemsChanged(IList<object> obj)
{
    // Implement logic here
}

That's it.

Points of Interest

This is our first implementation using this pattern. For now, you need to create one attached property and event for each event you want to publish. We're working on making this generic at the moment.

History

  • 2 July 2009 - Article uploaded

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

Jan-Erik Romoeren


Member

Occupation: Software Developer (Senior)
Company: Conecto AS
Location: Norway Norway

Björn Asplund


Member

Occupation: Software Developer
Company: Diversify Consulting Group AB
Location: Sweden Sweden

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jul 2009
Editor: Deeksha Shenoy
Copyright 2009 by Jan-Erik Romoeren, Björn Asplund
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project