控制边距属性取决于wpf中的另一个控制边距属性

时间:2014-08-13 15:26:27

标签: wpf wpf-controls margin

我试图在我的wpf应用程序中以这种方式在网格中显示两个画布控件。 second_canvas.margin.left first_canvas.margin.right 相同。 但由于保证金属性不能用作变量,因此无法完成。 所以我尝试了" 厚度"。但是我无法将 first_canvas.margin.right 转换为厚度变量,因为它是 double 类型。所以有没有解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可以编写专用值转换器:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace WpfMagic
{
    class EditableMarginConverter : IValueConverter
    {
        private double GetMarginPartValue(Thickness margin, string defaultValue, IDictionary<string, string> instructions)
        {
            string sourceValue;
            string value = instructions.TryGetValue(defaultValue, out sourceValue) ? sourceValue : defaultValue;

            double numericalValue;
            if (double.TryParse(value, out numericalValue))
            {
                return numericalValue;
            }

            return value == "left" ? margin.Left :
                   value == "top" ? margin.Top :
                   value == "right" ? margin.Right :
                   margin.Bottom;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Thickness inputMargin = (Thickness)value;

            IDictionary<string, string> instructions = (parameter as string).Split()
                                                                            .Select(s => s.Split('='))
                                                                            .ToDictionary(t => t[0].ToLowerInvariant(), t => t[1].ToLowerInvariant());            

            Thickness outputMargin = new Thickness
            {
                Left = GetMarginPartValue(inputMargin, "left", instructions),
                Top = GetMarginPartValue(inputMargin, "top", instructions),
                Right = GetMarginPartValue(inputMargin, "right", instructions),
                Bottom = GetMarginPartValue(inputMargin, "bottom", instructions)
            };

            return outputMargin;
        }

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

你会像这样使用它:

<StackPanel xmlns:local="clr-namespace:WpfMagic" Orientation="Horizontal">
    <StackPanel.Resources>
        <local:EditableMarginConverter x:Key="marginConverter"></local:EditableMarginConverter>
    </StackPanel.Resources>
    <Canvas x:Name="first_canvas"
            Width="100"
            Height="100"
            Margin="0 0 10 0"
            Background="Red">
    </Canvas>
    <Canvas x:Name="second_canvas"
            Width="100"
            Height="100"
            Background="Green"
            Margin="{Binding Margin,
                                ElementName=first_canvas,
                                Converter={StaticResource marginConverter},
                                ConverterParameter='Left=Right Right=0'}">
    </Canvas>
</StackPanel>

这是一种可重复使用的方式,但是如果它是一次性的,你可能应该使用一些程序管道,除非你是教条主义并希望拥有完美的XAML。

答案 1 :(得分:0)

我这样简单地实现了它......

int right = system.convert.ToInt32(first_canvas.margin.right)
second_canvas.margin = new thickness(left = right)