Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify ONLY the right (or left, top, bottom) value of a WPF control's Margin property?

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" />
like image 843
bugged87 Avatar asked Sep 28 '12 00:09

bugged87


People also ask

How do I set margins in WPF?

The Margin property of UIElement, which is parent class of all WPF controls is used to set the margin of a control. Margin property takes four numbers - Left, Top, Right and Bottom, that is margin to the left top and right bottom.

How do you give a margin in C#?

Step 1: Create a button using the Button() constructor is provided by the Button class. // Creating Button using Button class Button MyButton = new Button(); Step 2: After creating Button, set the Margin property of the Button provided by the Button class. // Set the margin of the button Mybutton.

Which of the following properties specifies a simple way for setting the margin properties in a single declaration?

The margin property defines the space around an HTML element. It is possible to use negative values to overlap content. It specifies a shorthand property for setting the margin properties in one declaration.

What is padding WPF?

Padding represents the distance between the side of the control (which can be the margin) and its content. The content depends on the type of the control. Margin is outside the UI element, while Padding is inside it. Next Recommended Reading WPF: Textblock Vs Label.


2 Answers

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>

like image 119
bugged87 Avatar answered Oct 02 '22 17:10

bugged87


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.

like image 20
kbo4sho88 Avatar answered Oct 02 '22 17:10

kbo4sho88