Change margin programmatically in WPF / C#
Solution 1:
test.Margin = new Thickness(0, -5, 0, 0);
Alignment, Margins and Padding Overview (MSDN)
FrameworkElement.Margin (MSDN)
Solution 2:
test.Margin = new Thickness(0, 0, 0, 0);
Solution 3:
test.Margin = new Thickness(-5);
Solution 4:
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.