89
MyControl.Margin.Left = 10;

Error:

Cannot modify the return value of 'System.Windows.FrameworkElement.Margin' because it is not a variable

H.B.
  • 166,899
  • 29
  • 327
  • 400
Giffyguy
  • 20,378
  • 34
  • 97
  • 168

9 Answers9

148

The problem is that Margin is a property, and its type (Thickness) is a value type. That means when you access the property you're getting a copy of the value back.

Even though you can change the value of the Thickness.Left property for a particular value (grr... mutable value types shouldn't exist), it wouldn't change the margin.

Instead, you'll need to set the Margin property to a new value. For instance (coincidentally the same code as Marc wrote):

Thickness margin = MyControl.Margin;
margin.Left = 10;
MyControl.Margin = margin;

As a note for library design, I would have vastly preferred it if Thickness were immutable, but with methods that returned a new value which was a copy of the original, but with one part replaced. Then you could write:

MyControl.Margin = MyControl.Margin.WithLeft(10);

No worrying about odd behaviour of mutable value types, nice and readable, all one expression...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    kudos for looking up the actual type... I confess to cheating with `var` - ahem, sorry; I mean "using an appropriate language feature" ;-p – Marc Gravell Jun 16 '09 at 20:35
  • 1
    It helped that the error message contained the fully qualified type name. With a quick bookmark for MSDN, I just needed "msdn System.Windows.FrameworkElement.Margin" on the address bar to get to the right page... – Jon Skeet Jun 16 '09 at 20:36
  • Marc, zvolkov has discovered our secret! It turned out it was way too easy to get 200 in a day, so I thought I'd set myself more of a challenge and try to get the top 2 users. Getting "Marc" as a moderator was just a bonus... ;) – Jon Skeet Jun 16 '09 at 20:52
  • But what about the others of us? I mean me? do they know about the "legion" script yet? – Marc Gravell Jun 16 '09 at 23:09
  • Can I add that you guys are hilarious? Do you work together outside of StackOverflow or something? You're all so chummy - it makes me feel like an outsider, haha. – Giffyguy Jun 17 '09 at 18:54
45

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);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • `New Thickness` is exactly what I was searching for when wanting to modify both the `Margin` and the `Padding` properties. I would wager this should work for `BorderThickness` as well. THANKS!! – k1dfr0std Jan 19 '22 at 08:51
17

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;
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
15

One could simply use this

MyControl.Margin = new System.Windows.Thickness(10, 0, 5, 0);
LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
4

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);
Ash
  • 5,057
  • 7
  • 35
  • 49
4

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

4

Depends on the situation, you can also try using padding property here...

MyControl.Margin=new Padding(0,0,0,0);
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Rishi
  • 41
  • 1
4

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

KalleP
  • 319
  • 4
  • 16
0

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
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • using inheritance to solve such problem? Quite a big overhead, isn't it? – LuckyLikey May 29 '17 at 06:58
  • @LuckyLikey it depends on the use case: if you plan to alter the `MerginLeft` on multiple places in the project, inheritance avoids design problems. Of course, if this is a single place in your project, I would choose the accepted answer. – Jan Turoň May 29 '17 at 13:00