I’ve always liked attached properties… so much so that a couple of years ago I created a series of generic classes to simplify the process of creating custom attached properties.
Attached Properties – a review:
- External control interactions — like the Grid.Row attached property.
- Basic info storage — just like the Tag property.
- Behaviors — changing properties and behaviors of the parent control.
- Bindings — to achieve custom bindings between UI controls.
- If you use XAML, you use attached properties whether you realize it or not.
Creating a custom attached property only takes a few lines of code. Many declarative types depend upon each other, such as in the example code below. Miss changing just one, and at best you’ll get an error. At worst, the code will run and produce unpredictable and hard-to-track-down behaviors. If you copy and paste, eventually you will forget to change one of the dependencies.
Generic Attached Properties – a review:
It was easy to take the custom attached property pattern and put a generic spin on it.
The generic classes that flowed from this base class have many advantages.
- Do a great job with encapsulation of functionality.
- Easy to use and extend.
- Strongly typed.
- Can be used with binding.
- Can be used in code behind and XAML
- Eliminates copy/paste errors.
To get a full accounting of the generic attached property classes that I created, you can review my previous blog entries on them.
- Using a Generic Custom Attached Property Part 1
- Using a Generic Custom Attached Property Part 2
- Associated Object Attached Property
Windows 8/Windows Phone 8 Update:
How do these classes work with Windows 8 and Windows Phone 8? With an update of the namespaces, the generic attached property classes absolutely work in both Windows 8 and Windows Phone 8. However, they do not work in XAML anymore, only code behind. For some that maybe an issue, but I’ve always been more of a code behind guy so it’s not as big of an issue for me.
Without a doubt, the generic class, AttachedPropertyAssociatedObject, is the workhorse of the Generic Attached Property framework. It allows you to easily add behaviors to any Framework element. Previous blog entries that put the AttachedPropertyAssociatedObject to work and show its versatility are listed here:
- Enabling/Disabling Telerik’s RadTabControl tab selection with a generic attached property
- Binding Expression Delay with a generic attached property
- Using Attached Properties to achieve a multibinding behavior in Silverlight
- Clipping any FrameworkElement Dynamically
After two years of continuous work, the AttachedPropertyAssociatedObject class gets an update as it starts its service to Window 8 and Window Phone 8.
The biggest change was to replace the complicated Dictionary framework with a simple ValueChanged attached property.
In the AttachedPropertyAssociatedObject.OnValueChanged handler, we check to see if there is a value assigned to the new ValueChanged attached property. If found, we call it (in red).
Let’s put it to some good use in Windows 8!
Windows 8 Settings Charm Pages Example:
We all know about the Charms bar in Windows 8 and that we should all start putting our settings pages under the Settings charm. Microsoft has a good example of adding these pages with their App settings sample. We’re not going to go into the details; that’s why we included the link to the sample. We’re going to assume you’re familiar with the process.
Adding new settings charm pages is not a hard concept, but there is a bunch of code to copy and paste. In short, this NEEDS to be handled by something based upon AttachedPropertyAssociatedObject.
We start off with a class to store the values of an instance of a settings charm page.
We might need more than one settings charm page so we need a collection class. Note the PopupChanged event. This is very important when using the AdControl, but that’s for another blog entry.
Now we get to the main class SettingsFlyoutAttachedProperty which is based upon AttachedPropertyAssociatedObject. Much of this code is just copied and pasted right out of the MS sample. The type of the attached property is the collection class we previously defined. Also note that we restrict the FrameworkElement attachment to a type of Page.
We handle the current view CommandsRequested event that gets triggered when the user selects the Settings Charm. We iterate through our collection and create a SettingsCommand instance for each of our desired pages. We also handle when we want to close any of our pages.
This is the meat of the class. Here we are handling the SettingsCommand event that gets called when the user selects an instance of one of our Settings Charm pages. This code is copied and pasted directly from the MS sample, with minor changes to read values from our supporting classes. Now the SettingsFlyoutAttachedProperty class is complete.
The actual page that the user sees must be based upon the LayoutAwarePage found in the MS sample. This class helps with, you guessed it, layout issues. From there you can add whatever content you need for your settings page.
Now that we have our generic attached property and our settings page, how much code do we need to initialize our page? Not much. We only need to create an instance of our collection class, add an instance of our info class, and set the value to our attached property class to our main page. Note that we clean up our pages if we navigate away from the main page.
Running the Windows 8 Settings Charm Pages Example:
When running the example you can see that our Settings Charm page is shown as an option.
Selecting our charm option causes our settings page to be shown.
Need more pages? Just create additional pages based upon LayoutAwarePage and add them to the collection.
Windows 8 Settings Charm Page Example – Try it yourself:
This example is available here via GitHub.
Now go and create your own setting charm pages.
Latest posts by Falafel Posts (see all)
- Matching Complex Query String Rewrite Rule in IIS - March 22, 2017
- Using Google Services in UWP C# Apps – Part 2 - February 7, 2017
- Using Google Services in UWP C# Apps – Part 1 - February 6, 2017
- Redis Caching in the Google Cloud Platform - February 3, 2017
- Entity Framework with Google Cloud SQL - February 2, 2017