绑定到多边形点的替代方法

huangapple go评论47阅读模式
英文:

Alternative for binding into Polygon.Points

问题

我想要绘制一个箭头。有一条线,线的中间有一个多边形。我不想为多边形单独创建一个 PointCollection,因为它取决于线的位置。所以我想到的解决方案是创建一个转换器,将线的中点 X1Y1X2Y2 转换为多边形的点。
ArrowViewModel
```csharp
        private double _X1;
        public double X1
        {
            get
            {
                return _X1;
            }
            set
            {
                SetProperty(ref _X1, value, () => X1);
            }
        }
      
        private double _Y1;
        public double Y1
        {
            get
            {
                return _Y1;
            }
            set
            {
                SetProperty(ref _Y1, value, () => Y1);
            }
        }
        private double _X2;
        public double X2
        {
            get
            {
                return _X2;
            }
            set
            {
                SetProperty(ref _X2, value, () => X2);
            }
        }
        private double _Y2;
        public double Y2
        {
            get
            {
                return _Y2;
            }
            set
            {
                SetProperty(ref _Y2, value, () => Y2);
            }
        }

这是我的视图:

<Polygon Fill="Black" >
            <Polygon.Points>
                <!--绑定到 X1,Y1,X2,Y2-->
            </Polygon.Points>
        </Polygon>

我尝试过多重绑定,但无法绑定到这些点。有没有其他的方法?


<details>
<summary>英文:</summary>

I want to draw an arrow. there is a line witch has a polygon at the middle of the line. I don&#39;t want to have a PointCollection separately for the polygon because it depends on the line location. So the solution I came up with was to make a converter and convert the middle of line X1,Y1,X2,Y2 to the polygon points.  
ArrowViewModel
    private double _X1;
    public double X1
    {
        get
        {
            return _X1;
        }
        set
        {
            SetProperty(ref _X1, value, () =&gt; X1);
        }
    }
  
    private double _Y1;
    public double Y1
    {
        get
        {
            return _Y1;
        }
        set
        {
            SetProperty(ref _Y1, value, () =&gt; Y1);
        }
    }
    private double _X2;
    public double X2
    {
        get
        {
            return _X2;
        }
        set
        {
            SetProperty(ref _X2, value, () =&gt; X2);
        }
    }
    private double _Y2;
    public double Y2
    {
        get
        {
            return _Y2;
        }
        set
        {
            SetProperty(ref _Y2, value, () =&gt; Y2);
        }
    }
and this is my view

<Polygon Fill="Black" >
<Polygon.Points>
//bind to X1,Y1,X2,Y2
</Polygon.Points>
</Polygon>





I tried multi binding but you can&#39;t bind to the points. Is there any alternative?

</details>


# 答案1
**得分**: 1

你需要以某种方式创建一个 `PointCollection`,因为你只能将 `Polygon` 的 `Points` 属性设置为 `PointCollection`。

可以在视图模型中创建一个 `PointCollection` 属性并直接绑定到它,或者在视图中使用 `MultiBinding` 和一个转换器,例如:

```csharp
public class PointsConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double x1 = (double)values[0];
        double y1 = (double)values[1];
        double x2 = (double)values[2];
        double y2 = (double)values[3];

        // 在这里添加你的计算逻辑
        Point[] points = new Point[] { ... };

        return new PointCollection(points);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

XAML:

<Polygon Fill="Black">
    <Polygon.Points>
        <MultiBinding>
            <MultiBinding.Converter>
                <local:PointsConverter />
            </MultiBinding.Converter>
            <Binding Path="X1" />
            <Binding Path="Y1" />
            <Binding Path="X2" />
            <Binding Path="Y2" />
        </MultiBinding>
    </Polygon.Points>
</Polygon>

(注意:代码部分不会被翻译,只返回翻译好的部分。)

英文:

You need to create a PointCollection one way or another because you can only set the Points property of a Polygon to a PointCollection.

Either create a PointCollection property in the view model and bind directly to this one, or use a MultiBinding and a converter in the view, e.g.:

public class PointsConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double x1 = (double)values[0];
        double y1 = (double)values[1];
        double x2 = (double)values[2];
        double y2 = (double)values[3];

        // your calculation logic here
        Point[] points = new Point[] { ... };

        return new PointCollection(points);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

XAML:

&lt;Polygon Fill=&quot;Black&quot; &gt;
    &lt;Polygon.Points&gt;
        &lt;MultiBinding&gt;
            &lt;MultiBinding.Converter&gt;
                &lt;local:PointsConverter /&gt;
            &lt;/MultiBinding.Converter&gt;
            &lt;Binding Path=&quot;X1&quot; /&gt;
            &lt;Binding Path=&quot;Y1&quot; /&gt;
            &lt;Binding Path=&quot;X2&quot; /&gt;
            &lt;Binding Path=&quot;Y2&quot; /&gt;
        &lt;/MultiBinding&gt;
    &lt;/Polygon.Points&gt;
&lt;/Polygon&gt;

huangapple
  • 本文由 发表于 2023年7月4日 22:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定