I can set margins individually in code but how do I do it in XAML, e.g. how do I do this:
PSEUDO-CODE:
<StackPanel Margin.Top="{Binding TopMargin}">
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The key is to realize that setting it in code like this:
is equivalent to:
You can’t set just a single value in a
Thicknessinstance through either code or XAML. If you don’t set some of the values, they will be implicitly zero. Therefore, you can just do this to convert the accepted code sample in your other question to a XAML equivalent:where
MyConverterjust returns aThicknessthat sets only theTopand leaves all other values as zero.Of course, you could write your own control that does expose these individual values as dependency properties to make your code a little cleaner:
A better option than a custom control would be to write an attached property and change the Thickness using the code above in the dependency property setter. The below code would be usable across ALL controls which have a Margin.
If you couple this with a Behavior, you can get notification changes on the TopMargin property.