2016-09-21 80 views
-4

我想单独绑定Point的X和Y属性,是否可行?
如果这一点是一个对象的属性,是否可行?
创建一个新的类并添加隐式转换为Point,是否可行?
(中国,英语不好,由谷歌翻译)我可以单独绑定xaml的Point和X属性吗?

像这样@Trifon

<!-- language: lang-c# --> 

public class BindingPoint : Animatable 
{ 
    public double X 
    { 
     get { return (double)GetValue(XProperty); } 
     set { SetValue(XProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty XProperty = 
     DependencyProperty.Register("MyProperty", typeof(double), typeof(BindingPoint), new PropertyMetadata(0.0)); 
    public double Y 
    { 
     get { return (double)GetValue(YProperty); } 
     set { SetValue(YProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty YProperty = 
     DependencyProperty.Register("MyProperty", typeof(double), typeof(BindingPoint), new PropertyMetadata(0.0)); 

    public BindingPoint() { } 
    public BindingPoint(double x, double y) 
    { 
     X = x; 
     Y = y; 
    } 

    public static implicit operator Point(BindingPoint bp) 
    { 
     return new Point(bp.X, bp.Y); 
    } 
} 

它工作在C#代码,如 “点P =新BindingPoint(1,1);”? 。
但它不适用于xaml代码!

<Path> 
    <Path.Data> 
     <LineGeometry> 
      <LineGeometry.StartPoint> 
       <!--Type must be "Point"--> 
       <local:BindingPoint X="10" Y="10"/> 
      </LineGeometry.StartPoint> 
     </LineGeometry> 
    </Path.Data> 
</Path> 

@Clemens欲结合y以一个改变值()为每个端点。
我该怎么办?

<Path StrokeThickness="2" Stroke="Cyan" Canvas.Left="300" xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
    <Path.Resources> 
     <sys:Double x:Key="value"/> 
    </Path.Resources> 
    <Path.Data> 
     <GeometryGroup> 
      <LineGeometry StartPoint="50,20"> 
       <LineGeometry.EndPoint> 
        <Point X="30" Y="{StaticResource value}"/> 
       </LineGeometry.EndPoint> 
      </LineGeometry> 
      <LineGeometry StartPoint="50,20"> 
       <LineGeometry.EndPoint> 
        <Point X="50" Y="{StaticResource value}"/> 
       </LineGeometry.EndPoint> 
      </LineGeometry> 
      <LineGeometry StartPoint="50,20"> 
       <LineGeometry.EndPoint> 
        <Point X="70" Y="{StaticResource value}"/> 
       </LineGeometry.EndPoint> 
      </LineGeometry> 
     </GeometryGroup> 
    </Path.Data> 
</Path> 
+0

不可以。 Point不是一个依赖对象。 Point.X和Point.Y不是依赖项属性。 –

+0

你为什么要这么做?显示XAML你需要绑定一个Point的X和Y. – Clemens

+0

'Y =“{StaticResource value}”'不是绑定。请准确告诉我们你实际上想要达到的目标。 – Clemens

回答

0

创建一个封装Point的新类。声明为新类X和Y的属性。然后,您将能够绑定新类的这些属性。

+0

是否这样?见下文。 – Flithor

相关问题