Change margin programmatically in WPF / C#

C#WpfXaml

C# Problem Overview


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#?

C# Solutions


Solution 1 - C#

Solution 2 - C#

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

Solution 3 - C#

test.Margin = new Thickness(-5);

Solution 4 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyozawiratamaView Question on Stackoverflow
Solution 1 - C#Eben GeerView Answer on Stackoverflow
Solution 2 - C#AndreyView Answer on Stackoverflow
Solution 3 - C#Kent BoogaartView Answer on Stackoverflow
Solution 4 - C#Shweta GoyalView Answer on Stackoverflow