views:

624

answers:

5

MyControl.Margin.Left = 10 ;

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

+1  A: 

See this MSDN social page with a very similar query - it looks like it has to do with structs versus classes. Good luck!

Mike
+2  A: 

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
+2  A: 

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
MyControl.Margin = margin;
TheMissingLINQ
(cheers, fixed some time ago... I expect you needed a page refresh ;-p)
Marc Gravell
Heh, if only we had Ajax comment updates so I didn't look like a tool...
TheMissingLINQ
+2  A: 

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
+10  A: 

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
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
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
coincidentally huh? ;)
zvolkov
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
But what about the others of us? I mean me? do they know about the "legion" script yet?
Marc Gravell
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