我怎样才能绑定一个属性到另一个属性,通过特定的偏移量?

问题描述:

我想要一条线的两个点与两个椭圆,这是内部ScatterViewItems。
此刻,我能够结合ActualCenter.X和.Y为ScatterViewItem,其工作方式,但由于椭圆不在该ScatterViewItem唯一的元素(也有一个标签),它的偏离中心,如该线的端点不是椭圆的中心

I'm trying to bind two points of a line to two Ellipses, which are inside ScatterViewItems. At the moment, I'm able to bind to ActualCenter.X and .Y for the ScatterViewItem, which works, but because the ellipse is not the only element in that ScatterViewItem (there is also a label), it's off-center, such that the line's endpoints are not the center of the ellipse.

椭圆不具有ActualCenter属性

Ellipse doesn't have an ActualCenter property.

我想知道我是否能够抵消属性一个固定的量结合,还是有一些其他类型的结合,我可以使用,这样我可以制定出在容器类正确的椭圆中心(为椭圆,标签和scatterviewitem),并返回作为结合的源

I wanted to know whether I'm able to offset a property binding by a fixed amount, or whether there is some other kind of binding I can use such that I can work out the correct ellipse center in the container class (for the ellipse, label and scatterviewitem), and return that as the source of the binding.

下面是code,其中我设置的绑定。 this.avatar 形状。 node_one node_two 是容器对象,它包含一个 ScatterViewItem 。在节点类也包含椭圆,该中心为此我将基本上想为源,但我满意的简单电流源的偏置

Below is the code where I am setting the binding. this.avatar refers to a Line shape. node_one and node_two are the container objects, which contain a ScatterViewItem. The Node class also contains the Ellipse, the centre for which I would essentially like as the source, but I'm happy with a simple offset of the current source.

        BindingOperations.SetBinding(this.avatar, Line.X1Property, new Binding
        {
            Source = node_one.getScatterViewItem(),
            Path = new PropertyPath("ActualCenter.X")
        });
        BindingOperations.SetBinding(this.avatar, Line.Y1Property, new Binding
        {
            Source = node_one.GetNodeCenterY(),
            Path = new PropertyPath("GetNodeCenterY()")
        });

        // Bind line.(X2,Y2) to destination.ActualCenter  
        BindingOperations.SetBinding(this.avatar, Line.X2Property, new Binding
        {
            Source = node_two.getScatterViewItem(),
            Path = new PropertyPath("ActualCenter.X")
        });
        BindingOperations.SetBinding(this.avatar, Line.Y2Property, new Binding
        {
            Source = node_two.GetNodeCenterY(),
            Path = new PropertyPath("GetNodeCenterY()")
        });

我试图设置为来源 node_one 路径新的PropertyPath(getOffsetCenter())返回一个双,但没有奏效。它没有抱怨,但它没有工作:)任何提示是AP preciated:)

I attempted to set to the Source as node_one and Path as new PropertyPath("getOffsetCenter()") which returns a double, but it didn't work. It didn't complain, but it didn't work :) Any tips are appreciated :)

编辑,尝试使用的IValueConverter:

Edit, trying to use IValueConverter:

[ValueConversion(typeof(Double), typeof(Double))]
public class OffsetValues : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime date = (DateTime)value;
        Double y = (Double)value;
        Double offset = (Double)parameter;
        return y + offset;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Double dblValue = (Double)value;
        Double offset = (Double)parameter;
        Double resultDblValue = dblValue - offset;
        return resultDblValue;
    }
}

上面添加一个新的类,但我会如何将其连接到我绑定的来源 - 在MSDN中的例子有一个基于XAML实现,但我的是所有方案

The above was added as a new class, but how would I attach it to the source of my binding - the example at MSDN has a XAML based implementation but mine is all programmatic.

您需要写一些ValueConverter在背后的code。

You need to write some ValueConverter in the code behind for that.

有关更多信息,看这里