Sometimes I think up things all by myself, sometimes I have to read something first to let the ole’ light bulb in my brain go ‘ping’. This post is of the 2nd category, and was inspired by this brilliant piece by C# MVP Iris Classon who is – apart from a very smart and respected coder – also a living and shining (not to mention colorful) axe at the root of the unfortunate gender bigotry that is still widespread in the IT world – a lot of people still think seem convinced “women can’t code, if they do they do it badly, and those who do are socially inept, boring and ugly”. Which is demonstrably untrue – coding requires just a brain and determination - but I am convinced it still shies a lot of brain power out of IT.
Anyway – what I basically did was take Iris’ idea an turn it into – you guessed it – a behavior, to make it more reusable.
To demonstrate how it works, I created the following Store app with a very compelling UI:
There is only one text of 75pt there. And then I created three Visual States:
- Default, which does basically nothing
- Medium, which changes the font size to 50pt
- Small, which changes the font size to 25pt
How this is done is amply explained by Iris’ post so I don’t feel very compelled to repeat that here. After creating the states I brought in my WpWinNl nuget package, and added my new SizeVisualStateChangeBehavior to the page. Then I added three entries to “SizeMappings” to the behavior by clicking on the indicated ellipses on the right:
- “Default” with width 801
- “Medium” with width 800
- “Small” with width 500
From 801 up to infinity the the Default state will be used (showing the text in it’s default size), between 800 and 501 the Medium state will be used (50pt), and from 500 and lower, the Small state (25pt). And voilá, automatic sizing of items done by filling in some boxes in Blend – or typing in some XAML in Visual Studio if that’s more of your thing. Notice you can add any number of Visual States for any range of widths, just as long as there is one “Default” state that has a width that’s one higher than the largest none-default width. Notice SizeVisualStateMappings can have any name you like as well, as long as they correspond with names of Visual States.
For extra credit, by the way, I made this behavior attachable to Control objects rather than Page, so it can be used inside (user) controls as well. And with some #ifdef directives it also works for Windows Phone, which might come in handy with more and more of resolutions entering the Windows Phone arena.
As to how this behavior works, it’s not quite rocket science. First, we have this hugely complex class :-P that holds one SizeVisualStateMapping:
namespace WpWinNl.Behaviors { public class SizeVisualStateMapping { public string VisualState { get; set; } public int Width { get; set; } } }
The actual behavior’s setup almost immediately shows what I am doing – I simply latch on to the SizeChanged event of the object that the behavior is attached to:
using System.Collections.Generic; using System.Diagnostics; using System.Linq; #if WINDOWS_PHONE using System.Windows; using System.Windows.Controls; #else using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; #endif namespace WpWinNl.Behaviors { public class SizeVisualStateChangeBehavior : SafeBehavior<Control> { protected override void OnSetup() { AssociatedObject.SizeChanged += AssociatedObjectSizeChanged; base.OnSetup(); UpdateVisualState(); } private void AssociatedObjectSizeChanged(object sender, SizeChangedEventArgs e) { UpdateVisualState(); } } }The core of the behavior’s functionality is the UpdateVisualState method:
private void UpdateVisualState() { if (SizeMappings != null) { SizeVisualStateMapping wantedMapping = null; var wantedMappings =
SizeMappings.Where(p => p.Width >= AssociatedObject.ActualWidth); if (wantedMappings.Any()) { wantedMapping = wantedMappings.OrderBy(p => p.Width).First(); } else { var orderedMappings = SizeMappings.OrderBy(p => p.Width); if (AssociatedObject.ActualWidth < orderedMappings.First().Width) { wantedMapping = orderedMappings.First(); } else if (AssociatedObject.ActualWidth > orderedMappings.Last().Width) { wantedMapping = orderedMappings.Last(); } } if (wantedMapping != null) { VisualStateManager.GoToState(AssociatedObject, wantedMapping.VisualState,
false); } } }
Which simply tries to find a SizeVisualStateMapping that’s fit for the current width of the associated object. If it finds that, it tells the VisualStateManager to go to that state, which proceeds to do the actual work. And that’s basically all. All that's left are an Dependency Property SizeMapping of type List<SizeVisualStateMapping> SizeMappings that holds the actual mapings, and a Cleanup method that detaches the behavior from the SizeChanged event again.
Full details, and a working solution including this behavior, can be found here. If you run this app in split screen and slowly makes it’s window width smaller, you will notice the text getting smaller. Be aware that the change only fires when you actually let the window divider control go – as long as you keep it active (by touching or dragging it) nothing will happen.
No comments:
Post a Comment