wpf 设置绑定的边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14360381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:04:21  来源:igfitidea点击:

Set a margin from a binding

c#wpfxamlbindingmargin

提问by user589195

I have a binding value that returns a int that represents a value I wasnt to assign to left and right margins of an element.

我有一个绑定值,它返回一个 int 值,该值表示我没有分配给元素的左右边距的值。

Heres what I've tried but it wont compile.

这是我尝试过的,但无法编译。

It works if I set the entire margin, but I only want left and right.

如果我设置整个边距,它会起作用,但我只想要左右。

Xaml:

Xml:

<Image x:Name="_image" Source="mat.png" Margin="{Binding EditorRow.BondIndent},0,{Binding EditorRow.BondIndent},0" />

class:

班级:

public int BondIndent
{
    get { return _bondSequence * 5; }
}

回答by Luuk

Return the margin?

退回保证金?

public Thickness Margin
{
    get { return new Thickness(BondIndent,0,BondIndent,0);}
}

Then change:

然后改变:

<Image x:Name="_image" Source="mat.png" Margin="{Binding EditorRow.Margin}" />

回答by odyss-jii

You probably need to use a ValueConverterfor this. Something like:

您可能需要为此使用ValueConverter。就像是:

public class LeftRightThicknessConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is int)
        {
            int margin = (int)value;
            return Thickness(margin, 0, margin, 0);
        }
        return Thickness();
    }

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

You can then use the converter in the following way:

然后,您可以按以下方式使用转换器:

<Grid>
    <Grid.Resources>
        <xxx:LeftRightThicknessConverter x:Key="LeftRightThicknessConverter" />
    </Grid.Resources>

    <Image Margin="{Binding SomePropertyPath, Converter={StaticResource LeftRightThicknessConverter}}" />
</Grid>

Assuming that xxxis a valid xml-namespace.

假设这xxx是一个有效的 xml 命名空间。

回答by Default

Instead of returning an intyou can return a Thickness, which the Marginactually is:

int您可以返回一个Thickness而不是返回一个,它Margin实际上是:

public Thickness BondIndent
{
    get
    {
        int margin = _bondSequence * 5;
        return new Thickness(margin, 0, margin, 0);
    }
}

The reason why your example works is because Thicknesshas overloaded constructors that take 1, 2 or 4 arguments. Whenthe constructor that takes 1 argument is called, all sides are initialized to that value. WPF automatically converts this to a Thicknessbased on the bound value.

您的示例工作的原因是因为Thickness重载了带有 1、2 或 4 个参数的构造函数。当调用带有 1 个参数的构造函数时,所有边都被初始化为该值。WPFThickness根据绑定值自动将其转换为 a 。

On another topic, BondIndentmight better be called BondMarginor BondThicknessnow.

在另一个主题上,BondIndent最好BondMargin还是BondThickness现在调用。

回答by RandomEngy

Just wrote some attached properties that should make it easy to set an individual Margin value from a binding or static resource:

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

WPF:

WPF:

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

    private static void LeftChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;
            frameworkElement.Margin = new Thickness((double)e.NewValue, currentMargin.Top, currentMargin.Right, currentMargin.Bottom);
        }
    }

    public static void SetLeft(UIElement element, double value)
    {
        element.SetValue(LeftProperty, value);
    }

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

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

    private static void TopChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;
            frameworkElement.Margin = new Thickness(currentMargin.Left, (double)e.NewValue, currentMargin.Right, currentMargin.Bottom);
        }
    }

    public static void SetTop(UIElement element, double value)
    {
        element.SetValue(TopProperty, value);
    }

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

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

    private static void RightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;
            frameworkElement.Margin = new Thickness(currentMargin.Left, currentMargin.Top, (double)e.NewValue, currentMargin.Bottom);
        }
    }

    public static void SetRight(UIElement element, double value)
    {
        element.SetValue(RightProperty, value);
    }

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

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

    private static void BottomChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var frameworkElement = d as FrameworkElement;
        if (frameworkElement != null)
        {
            Thickness currentMargin = frameworkElement.Margin;
            frameworkElement.Margin = new Thickness(currentMargin.Left, currentMargin.Top, currentMargin.Right, (double)e.NewValue);
        }
    }

    public static void SetBottom(UIElement element, double value)
    {
        element.SetValue(BottomProperty, value);
    }

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

UWP:

UWP:

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;
    }
}

Usage:

用法:

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

The nice thing is they won't override the other values on the Margin, so you can combine them as well.

好处是它们不会覆盖 Margin 上的其他值,因此您也可以将它们组合起来。