英文:
Alternative for binding into Polygon.Points
问题
我想要绘制一个箭头。有一条线,线的中间有一个多边形。我不想为多边形单独创建一个 PointCollection,因为它取决于线的位置。所以我想到的解决方案是创建一个转换器,将线的中点 X1,Y1,X2,Y2 转换为多边形的点。
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'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, () => 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);
}
}
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'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:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论