Here is another tip to make you XAML code less cluttered. One area that you may find it hard to write and read and maintain is Grid layout control. Defining RowDefinitions and ColumnDefinitions waste too much space and adds too unnecessary lines of code. Hence it become too complicated to handle it.
The solution I am sugessting here is to use some attached properties to define RowDefinitions and ColumnDefinitions in a smarter way.
You can fetch the code for attached properties here: https://gist.github.com/2234500
This code adds these attached properties to Grid
- Grid.Rows
- Grid.Columns
and adds these attached properties to UIElemnet
- Cell
Grid.Rows and Grid.Columns are same thing but one is for specifying rows and one is for columns. These two properties are simply text value which you can set it according to the following Rules:
- Separate items with semi colon(;) , Example "auto;auto;*"
- Use auto for adding an Auto sized row or column , Example "auto;auto"
- Specify * for adding an star sized row or column, Example "1*,2*, *"
- Specify only a number for adding a fixed size row or column, Example "200,200,100"
- Append # followed by a number to add multiple rows and columns of same sizing. "1*#4,auto"
- The number of starts in an item will multiplies star factor. Example "**,***,*" instead of "2*,3*,*"
XAML Sample:
<Grid s:Grid.Rows="auto;1*;auto;auto" s:Grid.Columns="*#5;auto"> ... </Grid>
Adds 4 rows to grid which are all Auto sized except the second row which occupies all remained space. Also adds 5 star columns followed by an Auto sized column.
Implementaion
As I said this is simply 3 attached properties which allows you to specify the rows and columns using a textual representation. Here is the function which converts the textual value into a GridLength value:
public static GridLength ParseGridLength(string text) { text = text.Trim(); if (text.ToLower() == "auto") return GridLength.Auto; if (text.Contains("*")) { var startCount = text.ToCharArray().Count(c => c == '*'); var pureNumber = text.Replace("*", ""); var ratio = string.IsNullOrWhiteSpace(pureNumber) ? 1 : double.Parse(pureNumber); return new GridLength(startCount * ratio, GridUnitType.Star); } var pixelsCount = double.Parse(text); return new GridLength(pixelsCount, GridUnitType.Pixel); }
And here is the function which makes # operator working:
public static IEnumerable<GridLength> Parse(string text) { if (text.Contains("#")) { var parts = text.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); var count = int.Parse(parts[1].Trim()); return Enumerable.Repeat(ParseGridLength(parts[0]), count); } else { return new[] { ParseGridLength(text) }; } }
And finally the attached property change callback:
var grid = d as System.Windows.Controls.Grid; var oldValue = e.OldValue as string; var newValue = e.NewValue as string; if (oldValue == null || newValue == null) return; if (oldValue != newValue) { grid.ColumnDefinitions.Clear(); newValue .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) .SelectMany(Parse) .Select(l => new ColumnDefinition { Width = l }) .ToList().ForEach(grid.ColumnDefinitions.Add); }
The above code is for Columns attached property but this is the same for Rows attached property as well.
The whole source code for these attached properties:
Great post; wonderful idea. This solves my problem of having to do two full Control Templates because I wanted to reorder the columns in a grid.
ReplyDeleteI got it working (there were a few issues) and added some new features that I needed. I tried to fork your Gist on GetHub, but that didn't work for me (some proxy server thing, I'm sure), so I created a new one:
https://gist.github.com/2378017
I'm totally using this in my code.
Nice to hear that, I always had read about the benefits of Open Source, but now I feel it. Your changes to the code are great. If you don't mind I am going to update my post to show your gist directly and add your comments.
ReplyDeleteGreat piece of code. Saves time, writting, and improves readability.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete(Sorry, IDK why, the code is not the same as I wrote, so I use LT and GT in the code)
ReplyDeleteUsing of VS2008 I had to change in the code (otherwise error message):
.SelectMany(Parse) ===> .SelectManyLTstring, GridDefinitionInformationGT(Parse)
Otherwise this is GREAT!