This is quite easy to do from code-behind:
var button = new Button();
var margin = button.Margin;
margin.Right = 10;
button.Margin = margin;
In XAML, however, I'm limited to the following:
<Button Margin="0,0,10,0" />
The problem with this is that now I've potentially overwritten the other margin values (i.e. left, top, bottom) by setting them to zero).
Is there any way to have XAML like the following?
<Button MarginRight="10" />
An attached property could be used. In fact, this is exactly the purpose of attached properties: accessing parent element properties or adding additional functionality to a specific element.
For example, define the following class somewhere in your application:
Now in your XAML all you must do is declare the following namespace:
And then you can write XAML such as the following:
Alternatively, you can avoid using an attached property and instead write some slightly more lengthy XAML such as:<Button>
<Button.Margin>
<Thickness Right="10" />
</Button.Margin>
</Button>
You are wrong with this part:
Is already not valid code, because Margin returns a struct and is therefore a value type. And because it doesn't derive from DependencyObject a lot of DataBinding tricks also won't work.
I just wanted to give a proper explanation, otherwise i would say your first answer is pretty much the only way.
You could data bind the Margin to a property (string) in your MVVM. In your MVVM, all you need is to track the individual properties (top, right, bottom, right).
You can use converters as in: How to set a top margin only in XAML? or Binding only part of the margin property of WPF control
Although an attatched property can work. I would try to refactor your code so you are not making UI changes in the the code behind. You should handle as much of the UI as you can inside the design side of the file. I try to use the codebehind of xaml files as little as possible because it causes issues with MVVM.