One could simply use this
MyControl.Margin = new System.Windows.Thickness(10, 0, 5, 0);
Margin
is returning a struct, which means that you are editing a copy. You will need something like:
var margin = MyControl.Margin;
margin.Left = 10;
MyControl.Margin = margin;
It's a bit unclear what are you asking, but to make things comfortable, you can inherit your own Control and add a property with the code that Marc suggests:
class MyImage : Image {
private Thickness thickness;
public double MarginLeft {
get { return Margin.Left; }
set { thickness = Margin; thickness.Left = value; Margin = thickness; }
}
}
Then in the client code you can write just
MyImage img = new MyImage();
img.MarginLeft = 10;
MessageBox.Show(img.Margin.Left.ToString()); // or img.MarginLeft
One would guess that (and my WPF is a little rusty right now) that Margin takes an object and cannot be directly changed.
e.g
MyControl.Margin = new Margin(10,0,0,0);
To use Thickness
you need to create/change your project .NET framework
platform version to 4.5. becaus this method available only in version 4.5. (Also you can just download PresentationFramework.dll and give referense to this dll, without create/change your .NET framework
version to 4.5.)
But if you want to do this simple, You can use this code:
MyControl.Margin = new Padding(int left, int top, int right, int bottom);
also
MyControl.Margin = new Padding(int all);
This is simple and no needs any changes to your project
The Margin
property returns a Thickness
structure, of which Left
is a property. What the statement does is copying the structure value from the Margin
property and setting the Left
property value on the copy. You get an error because the value that you set will not be stored back into the Margin
property.
(Earlier versions of C# would just let you do it without complaining, causing a lot of questions in newsgroups and forums on why a statement like that had no effect at all...)
To set the property you would need to get the Thickness
structure from the Margin
property, set the value and store it back:
Thickness m = MyControl.Margin;
m.Left = 10;
MyControl.Margin = m;
If you are going to set all the margins, just create a Thickness
structure and set them all at once:
MyControl.Margin = new Thickness(10, 10, 10, 10);
Margin = new Thickness(0, 0, 0, 0);
Depends on the situation, you can also try using padding property here...
MyControl.Margin=new Padding(0,0,0,0);
Source: Stackoverflow.com