52

For this xaml:

<WebBrowser Name="test" Margin="0,0,0,0" />

How can I change the web browser control margin on top to be -5 programmatically in C#?

4 Answers 4

118
test.Margin = new Thickness(0, -5, 0, 0);

Alignment, Margins and Padding Overview (MSDN)
FrameworkElement.Margin (MSDN)

1
  • this also works with variables for the individual values, as long as they are of the Doubles type.
    – tCoe
    May 9, 2018 at 15:31
4
test.Margin = new Thickness(0, 0, 0, 0);
3
test.Margin = new Thickness(-5);
2

You can access the control from code behind using the Name property. In this case, test.Margin property can be used to change it dynamically.

Margin is set as thickness, so the solution could be:

test.Margin = new Thickness(0,-5,0,0);

Note: Thickness have 4 parameters viz left, top, right and bottom. In above solution, we have just changed top margin, rest remained unchanged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.