views:

180

answers:

2

I’ve got a Silverlight 3 DataForm which is adding some odd padding to the DataTemplate. Using Silverlight Spy to inspect the XAML output, it looks like there’s a ContentPresenter with a margin of 12, 12, 6, 12 which then contains the contents of the DataTemplate. This means there's always padding in the form which is not ideal for my situation.

Does anyone know how to adjust this margin without creating a style resource and templating the entire control? It doesn’t appear to be any of the obvious padding attributes of the form.

Here’s how it looks in Silverlight Spy: http://i169.photobucket.com/albums/u217/troyhunt/Silverlight.png?t=1249271108

A: 

I feel dirty for doing it, but I've just put negative margins on the first child element in the dataform. Very inelegant but gets the job done in light of no obvious alternatives.

Troy Hunt
A: 

An alternative way by using a small piece of code is to use the visual tree to find the ContentPresenter to set is margin.

Adapting the Finding Elements code to remove ContentElement that does not apply to Silverlight you can then find the content presenter and update the Margin e.g.

foreach ( var match in LayoutRoot.FindChildren<ContentPresenter>())
        {
            if ( match.Margin.Bottom == 12 && match.Margin.Top == 12 && match.Margin.Left == 12 && match.Margin.Right == 6)
                match.Margin = new Thickness(0);
        }
Grant Archibald