设置绑定边距

时间:2013-01-16 14:13:54

标签: c# wpf xaml binding margin

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

这是我尝试过的,但它不会编译。

如果我设置了整个边距,它可以工作,但我只想要左右。

的Xaml:

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

类:

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

4 个答案:

答案 0 :(得分:21)

退还保证金?

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

然后改变:

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

答案 1 :(得分:13)

您可能需要使用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();
    }
}

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

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

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

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

答案 2 :(得分:2)

您可以返回intMargin实际上是public Thickness BondIndent { get { int margin = _bondSequence * 5; return new Thickness(margin, 0, margin, 0); } } ,而不是返回Thickness

Thickness

您的示例之所以有效,是因为BondIndent重载的构造函数需要1,2或4个参数。当调用带有1个参数的构造函数时,所有边都被初始化为该值。 WPF会根据绑定值自动将其转换为BondMargin

关于另一个主题,BondThickness现在最好称为{{1}}或{{1}}。

答案 3 :(得分:1)

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

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:

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" />

好消息是他们不会覆盖保证金上的其他值,所以你也可以将它们组合起来。