52

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

伪代码:

<StackPanel Margin.Top="{Binding TopMargin}">
4

12 回答 12

57

这不是你要找的吗?

<StackPanel Margin="0,10,0,0" />

第一个值是左边距,然后是顶部,然后是右边,最后但并非最不重要的是底部。

我不确定你是否想将它绑定到某些东西,但如果没有,那会起作用。

于 2009-08-23T08:04:48.020 回答
45

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

sp2.Margin = new System.Windows.Thickness{ Left = 5 };

相当于:

sp2.Margin = new System.Windows.Thickness{ Left = 5, Top = 0, Right = 0, Bottom = 0 };

不能通过代码或 XAMLThickness在实例中仅设置一个值。如果您不设置某些值,它们将隐式为零。因此,您只需执行此操作即可将其他问题中接受的代码示例转换为 XAML 等效项:

<StackPanel Margin="{Binding TopMargin, Converter={StaticResource MyConverter}}"/>

whereMyConverter只返回 a Thickness,它只设置 theTop并将所有其他值保留为零。

当然,您可以编写自己的控件,这些单独的值公开为依赖属性,以使您的代码更简洁:

<CustomBorder TopMargin="{Binding TopMargin}">
</CustomBorder>

比自定义控件更好的选择是编写附加属性并使用依赖属性设置器中的上述代码更改厚度。下面的代码可用于所有具有 Margin 的控件。

public static readonly DependencyProperty TopMarginProperty =
    DependencyProperty.RegisterAttached("TopMargin", typeof(int), typeof(FrameworkElement),
                                        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public static void SetTopMargin(FrameworkElement element, int value)
{
    // set top margin in element.Margin
}
public static int GetTopMargin(FrameworkElement element)
{
    // get top margin from element.Margin
}

如果将此与 Behavior 结合使用,则可以在 TopMargin 属性上获得通知更改。

于 2009-08-22T18:21:17.150 回答
22

这属于 WPF/XAML 戒律:

  1. 我是 WPF/XAML,你的 UI 框架,你最终会在为 Windows 编写应用程序时使用我。
  2. 你应该没有其他技术 - 我不会跨平台,但我会尝试使用银光UWP,因为 Hololens 总有一天会变得巨大。“Xamarin.Forms”?从来没有听说过!
  3. 在使用 WPF/XAML 时,你将不可避免地反复妄称主的名。
  4. 记住安息日:每 7 天……或者几小时或几分钟的编码,我会让你休息一下,去StackOverflow2000things
  5. 孝敬父母:Windows 窗体。
  6. 如果你要采用 MVVM,你也应该实现INPCINCC,但是很高兴!你有一个选择:你可以使用它,或者你可以愤怒地使用它。
  7. 汝不应犯奸淫与其他应用程序和框架互操作。
  8. 你不应该觊觎你邻居的 UI 框架。
  9. 如果不编写几行代码隐藏,您将无法使用附加属性或边距的绑定动态设置元素的位置。
  10. 你永远不会bool在 XAML 中拥有简单的 Visibility 属性。我是 WPF/XAML。

你的罪名列在#9。

于 2012-07-07T16:00:41.687 回答
4

刚刚写了一些附加属性,应该可以很容易地从绑定或静态资源中设置单个 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;
    }
}

用法:

<TextBlock Text="Test"
    app:Margin.Top="{Binding MyValue}"
    app:Margin.Right="{StaticResource MyResource}"
    app:Margin.Bottom="20" />

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

于 2015-09-05T00:50:52.427 回答
2

您不能只使用绑定定义上边距,因为Margin它的类型Thickness不是依赖对象。但是,您可以使用MultiValueConverter需要 4 个边距值来制作 1 个厚度对象

转换器:

public class ThicknessMultiConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double left = System.Convert.ToDouble(values[0]);
        double top = System.Convert.ToDouble(values[1]);
        double right = System.Convert.ToDouble(values[2]);
        double bottom = System.Convert.ToDouble(values[3]);
        return new Thickness(left, top, right, bottom);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        Thickness thickness = (Thickness)value;
        return new object[]
        {
            thickness.Left,
            thickness.Top,
            thickness.Right,
            thickness.Bottom
        };
    }

    #endregion
}

XAML:

<StackPanel>
    <StackPanel.Margin>
        <MultiBinding Converter="{StaticResource myThicknessConverter}">
            <Binding Path="LeftMargin"/>
            <Binding Path="TopMargin"/>
            <Binding Path="RightMargin"/>
            <Binding Path="BottomMargin"/>
        </MultiBinding>
    </StackPanel.Margin>
</StackPanel>
于 2009-08-22T17:19:56.667 回答
1

这是一种无需编写转换器或硬编码边距值的简单方法。首先,在您的 Window(或其他控件)资源中定义以下内容:

<Window.Resources>
    <!-- Define the default amount of space -->
    <system:Double x:Key="Space">10.0</system:Double>

    <!-- Border space around a control -->
    <Thickness
        x:Key="BorderSpace"
        Left="{StaticResource Space}"
        Top="{StaticResource Space}"
        Right="{StaticResource Space}"
        Bottom="{StaticResource Space}"
        />

    <!-- Space between controls that are positioned vertically -->
    <Thickness
        x:Key="TopSpace"
        Top="{StaticResource Space}"
        />
</Window.Resources>

注意system定义为xmlns:system="clr-namespace:System;assembly=mscorlib"

现在您可以按如下方式使用这些资源:

<Grid
    Margin="{StaticResource BorderSpace}"
    >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Button
        Grid.Row="0"
        Content="Button 1"
        />

    <Button
        Grid.Row="1"
        Content="Button 2"
        Margin="{StaticResource TopSpace}"
        />
</Grid>

现在如果你想改变控件之间的默认空间,你只需要在一个地方改变它。

于 2015-07-07T21:24:26.217 回答
0

使用转换器,下面的示例代码会将您绑定的双精度转换为厚度。它将厚度的“顶部”设置为绑定字段。您可以选择使用 ConverterParameter 来确定您是绑定到左侧、顶部、右侧还是底部。

<StackPanel Margin="{Binding TopMargin, Converter={StaticResource MyThicknessConverter}">

.

public class ThicknessSingleValueConverter : IValueConverter
{
    override Convert(...)
    {
         return new Thickness(0, (double)object, 0, 0);
    }

    //etc...
于 2009-08-22T18:03:14.800 回答
0

我以为您可以使用MSDN中的属性语法:

      <object.Margin>
        <Thickness Top="{Binding Top}"/>
      </object.Margin>

比你不需要任何转换器

但顶部不是 DependancyProperty - 返回转换器

于 2009-10-13T08:42:30.577 回答
0

也许我“迟到了”,但不喜欢任何提供的解决方案,在我看来,最简单和最干净的解决方案是在 ViewModel (或您绑定的任何东西)中定义厚度属性,然后绑定该属性。像这样的东西:

public class ItemViewModel
{
  public Thickness Margin { get; private set }

  public ItemViewModel(ModelClass model)
  {
    /// You can calculate needed margin here, 
    /// probably depending on some value from the Model
    this.Margin = new Thickness(0,model.TopMargin,0,0);
  }
}

然后 XAML 很简单:

<StackPanel Margin="{Binding Margin}">
于 2014-07-15T13:12:58.477 回答
0

这是一个不错的解决方案:

        public class Nifty
    {
        private static double _tiny;
        private static double _small;
        private static double _medium;
        private static double _large;
        private static double _huge;
        private static bool _resourcesLoaded;

        #region Margins

        public static readonly DependencyProperty MarginProperty =
            DependencyProperty.RegisterAttached("Margin", typeof(string), typeof(Nifty),
                new PropertyMetadata(string.Empty,
                    new PropertyChangedCallback(OnMarginChanged)));

        public static Control GetMargin(DependencyObject d)
        {
            return (Control)d.GetValue(MarginProperty);
        }

        public static void SetMargin(DependencyObject d, string value)
        {
            d.SetValue(MarginProperty, value);
        }

        private static void OnMarginChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement ctrl = d as FrameworkElement;
            if (ctrl == null)
                return;

            string Margin = (string)d.GetValue(MarginProperty);

            ctrl.Margin = ConvertToThickness(Margin);
        }

        private static Thickness ConvertToThickness(string Margin)
        {
            var result = new Thickness();

            if (!_resourcesLoaded)
            {
                _tiny = (double)Application.Current.FindResource("TinySpace");
                _small = (double)Application.Current.FindResource("SmallSpace");
                _medium = (double)Application.Current.FindResource("MediumSpace");
                _large = (double)Application.Current.FindResource("LargeSpace");
                _huge = (double)Application.Current.FindResource("HugeSpace");

                _resourcesLoaded = true;
            }

            result.Left = CharToThickness(Margin[0]);
            result.Top = CharToThickness(Margin[1]);
            result.Bottom = CharToThickness(Margin[2]);
            result.Right = CharToThickness(Margin[3]);

            return result;
        }


        private static double CharToThickness(char p)
        {
            switch (p)
            {
                case 't':
                case 'T':
                    return _tiny;
                case 's':
                case 'S':
                    return _small;
                case 'm':
                case 'M':
                    return _medium;
                case 'l':
                case 'L':
                    return _large;
                case 'h':
                case 'H':
                    return _huge;
                default:
                    return 0.0;
            }
        }

        #endregion

    }

如果将此代码添加到命名空间并定义以下大小:

    <system:Double x:Key="TinySpace">2</system:Double>
<system:Double x:Key="SmallSpace">5</system:Double>
<system:Double x:Key="MediumSpace">10</system:Double>
<system:Double x:Key="LargeSpace">20</system:Double>
<system:Double x:Key="HugeSpace">20</system:Double>

然后,您可以像这样创建 Tiny、Small、Medium、Large 和 Huge 边距:

local:Nifty.Margin="H000"

或者

local:Nifty.Margin="_S_S"

然后,代码将根据您的资源创建边距。

于 2011-01-21T16:18:07.177 回答
0

我使用绑定到 Margin (RelativeSource Self) 的 ValueConverter 并解析 ConverterParameter,给出为“top:123;left:456”。

转换器仅覆盖参数给定的边距。

public class MarginConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is Thickness)) return new Thickness();

        Thickness retMargin = (Thickness) value;
        List<string> singleMargins = (parameter as string)?.Split(';').ToList() ?? new List<string>();

        singleMargins.ForEach(m => {
                                  switch (m.Split(':').ToList()[0].ToLower().Trim()) {
                                      case "left":
                                          retMargin.Left = double.Parse(m.Split(':').ToList()[1].Trim());
                                          break;
                                      case "top":
                                          retMargin.Top = double.Parse(m.Split(':').ToList()[1].Trim());
                                          break;
                                      case "right":
                                          retMargin.Right = double.Parse(m.Split(':').ToList()[1].Trim());
                                          break;
                                      case "bottom":
                                          retMargin.Bottom = double.Parse(m.Split(':').ToList()[1].Trim());
                                          break;
                                  }
                              }
            );
        return retMargin;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

xml

<TextBlock Margin="{Binding RelativeSource={RelativeSource Self}, 
                    Path=Margin, 
                    Converter={StaticResource MarginConverter}, 
                    ConverterParameter='top:0'}" 
Style="{StaticResource Header}" 
Text="My Header" />

TextBlock 将使用 Style 给出的 Margin,除了 Margin-Top,它将被 0 覆盖。

玩得开心!

于 2016-08-23T06:36:55.570 回答
-3

如果能够通过指定类似于下面的代码示例的内容来做到这一点,那就太好了。

<StackPanel Margin=",10,,">

不幸的是,默认情况下 WPF 中似乎不存在此功能,这是一种耻辱,因为它要求开发人员对已知的默认值进行硬编码,这使得以后对应用程序进行皮肤或主题化变得更加困难。

在这一点上我能想到的最好的解决方案是使用转换器,但是您必须生成的额外代码量来引入它并不理想。

于 2010-01-28T14:27:23.370 回答