如何仅在XAML中设置上边距?

Edw*_*uay 44 c# wpf xaml margins

我可以在代码中单独设置边距,但是如何在XAML中设置边距,例如我该怎么做:

伪代码:

<StackPanel Margin.Top="{Binding TopMargin}">
Run Code Online (Sandbox Code Playgroud)

Car*_*rlo 52

这不是你想要的吗?

<StackPanel Margin="0,10,0,0" />
Run Code Online (Sandbox Code Playgroud)

第一个值是左边距,然后是顶部,然后是右边,最后一个但不是最小值.

我不确定你是否想将它绑定到某个东西,但如果没有,那就行了.

  • 否。这会用“0”覆盖左/右/下。OP 希望保留现有的边距(可能来自样式绑定)并仅设置一侧。我也会这样做,但看来如果不付出相当大的努力你就做不到。 (3认同)

Ken*_*art 37

关键是要意识到在代码中设置它是这样的:

sp2.Margin = new System.Windows.Thickness{ Left = 5 };
Run Code Online (Sandbox Code Playgroud)

相当于:

sp2.Margin = new System.Windows.Thickness{ Left = 5, Top = 0, Right = 0, Bottom = 0 };
Run Code Online (Sandbox Code Playgroud)

不能通过代码或XAMLThickness实例中设置单个值.如果您没有设置某些值,它们将隐式为零.因此,您可以这样做将您在其他问题中接受的代码示例转换为XAML等效项:

<StackPanel Margin="{Binding TopMargin, Converter={StaticResource MyConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

其中,MyConverter刚刚返回Thickness的是只设置Top并留下所有其他值为零.

当然,你可以写你自己的控件,公开为依赖属性,这些单独的值,使你的代码干净了一点:

<CustomBorder TopMargin="{Binding TopMargin}">
</CustomBorder>
Run Code Online (Sandbox Code Playgroud)

  • 你可以像这样设置 `var margin = sp2.Margin; 边距.左 = 5; sp2.Margin = margin;` 这将使其他值保持不变。 (2认同)
  • @bugged87:OP 想在 XAML 中完成它。 (2认同)

G.Y*_*G.Y 22

这属于WPF修正案:

  1. 我是WPF,你会在编写Windows应用程序时使用我 - 最终.
  2. 不要使用其他技术 - 我不会跨平台,但我会尝试使用SL.
  3. 如果您打算使用我 - 请确保您知道自己在做什么.
  4. 每7天或几小时或几分钟的编码,我会让你休息一下去.
  5. 尊重窗体.
  6. MVVM - > INPC,INCC - >您可以使用它,也可以将它与愤怒一起使用 - 您的选择!
  7. 不要互操作其他应用程序.
  8. 您也应支付混合费用.
  9. 您无法使用附加属性或边距的绑定动态设置元素的位置,而无需在后面写几行代码.

  10. 不要将此技术与其他技术进行比较.

您的问题列在#9.


Ran*_*ngy 6

刚刚编写了一些附加属性,可以轻松地从绑定或静态资源设置单独的 Margin 值:

public class Margin
{
    public static readonly DependencyProperty LeftProperty = DependencyProperty.RegisterAttached(
        "Left",
        typeof(double),
        typeof(Margin),
        new PropertyMetadata(0.0));

    public static void SetLeft(UIElement element, double value)
    {
        var frameworkElement = element as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;

            frameworkElement.Margin = new Thickness(value, currentMargin.Top, currentMargin.Right, currentMargin.Bottom);
        }
    }

    public static double GetLeft(UIElement element)
    {
        return 0;
    }

    public static readonly DependencyProperty TopProperty = DependencyProperty.RegisterAttached(
        "Top",
        typeof(double),
        typeof(Margin),
        new PropertyMetadata(0.0));

    public static void SetTop(UIElement element, double value)
    {
        var frameworkElement = element as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;

            frameworkElement.Margin = new Thickness(currentMargin.Left, value, currentMargin.Right, currentMargin.Bottom);
        }
    }

    public static double GetTop(UIElement element)
    {
        return 0;
    }

    public static readonly DependencyProperty RightProperty = DependencyProperty.RegisterAttached(
        "Right",
        typeof(double),
        typeof(Margin),
        new PropertyMetadata(0.0));

    public static void SetRight(UIElement element, double value)
    {
        var frameworkElement = element as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;

            frameworkElement.Margin = new Thickness(currentMargin.Left, currentMargin.Top, value, currentMargin.Bottom);
        }
    }

    public static double GetRight(UIElement element)
    {
        return 0;
    }

    public static readonly DependencyProperty BottomProperty = DependencyProperty.RegisterAttached(
        "Bottom",
        typeof(double),
        typeof(Margin),
        new PropertyMetadata(0.0));

    public static void SetBottom(UIElement element, double value)
    {
        var frameworkElement = element as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;

            frameworkElement.Margin = new Thickness(currentMargin.Left, currentMargin.Top, currentMargin.Right, value);
        }
    }

    public static double GetBottom(UIElement element)
    {
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

<TextBlock Text="Test"
    app:Margin.Top="{Binding MyValue}"
    app:Margin.Right="{StaticResource MyResource}"
    app:Margin.Bottom="20" />
Run Code Online (Sandbox Code Playgroud)

在 UWP 中进行了测试,但这应该适用于任何基于 XAML 的框架。好处是它们不会覆盖边距上的其他值,因此您也可以将它们组合起来。