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:
using System;
using System.Windows;
using System.Windows.Controls;
namespace YourApp.AttachedProperties
{
public class MoreProps
{
public static readonly DependencyProperty MarginRightProperty = DependencyProperty.RegisterAttached(
"MarginRight",
typeof(string),
typeof(MoreProps),
new UIPropertyMetadata(OnMarginRightPropertyChanged));
public static string GetMarginRight(FrameworkElement element)
{
return (string)element.GetValue(MarginRightProperty);
}
public static void SetMarginRight(FrameworkElement element, string value)
{
element.SetValue(MarginRightProperty, value);
}
private static void OnMarginRightPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var element = obj as FrameworkElement;
if (element != null)
{
int value;
if (Int32.TryParse((string)args.NewValue, out value))
{
var margin = element.Margin;
margin.Right = value;
element.Margin = margin;
}
}
}
}
}
Now in your XAML all you must do is declare the following namespace:
xmlns:ap="clr-namespace:YourApp.AttachedProperties"
And then you can write XAML such as the following:
<Button ap:MoreProps.MarginRight="10" />
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>