I never expected that I’d write a post about Windows Phone but, here we are. I’m new to Windows Phone. I’ve done quite a lot of Android development and a little bit of iOS as well. Now it was time to take a look at Windows Phone, for a potential customer project. After a while I realized that I was going to need to create a custom control for the calendar that was to be shown in the app. No calendar control is available in the Windows Phone SDK so I decided to roll my own. I needed a month layout so I started by creating a Grid to hold the 7 x 6 Button objects, one Button for each day of the month (yes, there are reasons for having 42 buttons). The buttons should lie edge to edge like in this example:

Calendar
However, I realized that a Button object has some styling that makes it have a margin of 13px, which means that instead of having a grid like appearence, I would have a number of rectangles on the screen. There was no obviuos way to remove this margin, until I finally stumbled upon a solution.
The first step is to realize that there are no attributes for the button that can be changed in the XAML. What actually needs to be done is to create a new style for the Button objects. This is actually rather easy and can be done from either Visual Studio or Blend. Just right click an existing Button object and choose Edit template -> Edit a copy. This will insert a large chunk of XAML code into your project. In this generated code, search for the Border element. It will have an attribute Margin="{StaticResource PhoneTouchTargetOverhang}"
. Change that to Margin="0"
and you will have a Button style with no margins. Now, that by itself is not enough (did you think this would be easy?) No, now you have to apply the style to an actual Button:
<Button Content="Button" Style="{StaticResource ButtonStyle1}"/>
In this case, the name of the style is ButtonStyle1. Now you should have a fully functioning button, without any empty space around it. If you need more buttons in the same style, just add the style attribute to them.
Follow me on twitter here.