wpf - 创建数据绑定(bind)三角形?

标签 wpf data-binding shapes

我需要根据提供的尺寸创建多个形状(所有形状都具有相同的高度/宽度),并将它们的尺寸数据绑定(bind)到数据上下文上提供的属性。

大多数形状都很简单:圆形(具有高度/宽度限制的椭圆形)、方形(具有高度/宽度限制的矩形)、菱形(与方形相同,然后使用旋转变换)、+(两条线)、X (两行)。

但是我试图弄清楚如何对三角形执行此操作,但我无法弄清楚。它需要是一个填充的对象,所以我不能只用三行来完成。

但是我见过的所有方法(使用 PathPolygon)最终都会采用 Point 对象(StartPointEndPoint 等)。并且您无法绑定(bind)到 Point 对象的 XY 值。

我错过了什么吗?或者我需要编写自己的自定义形状或其他东西吗?

编辑:为了增加一点清晰度......我创建的三角形类型并不重要。它可以是等边或等腰。我的目标是等腰,这样它就有一个具有数据绑定(bind)宽度的底边,并且三角形的顶部“尖端”将位于数据绑定(bind)宽度的中点且 Y=0 处。这只是为了简单起见的优化

最佳答案

行为类别:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;

namespace WpfApplication1
{
    public enum ShapeType
    {
        Rectangle,
        Isosceles,
        Ellipse,
        Dice,
        Hexagon
    }

    public class PathControl
    {
        public static readonly DependencyProperty ShapeTypeProperty =
            DependencyProperty.RegisterAttached("ShapeType",
            typeof(ShapeType?),
            typeof(DependencyObject),
            new PropertyMetadata(null, 
                new PropertyChangedCallback((sender, args) => 
                { 
                    Path path = sender as Path;
                    ShapeType? shapeType = (ShapeType?)args.NewValue;

                    //todo: use a WeakEvent
                    path.SizeChanged += 
                        (pathSender, pathArgs) => 
                        {
                            PathControl.InvalidatePath((Path)sender, shapeType);
                        };

                })));


        private static void InvalidatePath(Path path, ShapeType? shapeType)
        {
            if (path != null
                && shapeType.HasValue)
            {
                string source = null;

                double netWidth = path.Width - 2 * path.StrokeThickness,
                       netHeight = path.Height - 2 * path.StrokeThickness;

                if (shapeType == ShapeType.Rectangle)
                {
                    source = string.Format("M0,0 h{0} v{1} h-{0} z",
                        new object[2]
                        {
                            netWidth,
                            netHeight
                        });
                }
                else if (shapeType == ShapeType.Isosceles)
                {
                    source = string.Format("M0,{1} l{0},-{1} {0},{1} z",
                        new object[2]
                        {
                            netWidth / 2,
                            netHeight
                        });
                }
                else
                {
                    throw new NotImplementedException(shapeType.ToString());
                }

                path.Data = Geometry.Parse(source);
            }
        }

        public static void SetShapeType(DependencyObject o, ShapeType e)
        {
            o.SetValue(PathControl.ShapeTypeProperty, e);
        }

        public static ShapeType? GetShapeType(DependencyObject o)
        {
            return (ShapeType)o.GetValue(PathControl.ShapeTypeProperty);
        }
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <Path Width="100" Height="100" Stroke="Green" StrokeThickness="2" Fill="Yellow"
              local:PathControl.ShapeType="Isosceles">
            <Path.RenderTransform>
                <RotateTransform Angle="90"></RotateTransform>
            </Path.RenderTransform>
        </Path>
    </Grid>
</Window>

关于wpf - 创建数据绑定(bind)三角形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8055552/

相关文章:

c# - WPF - 使用鼠标事件在 Canvas 上绘图

c# - 使用 clickonce 安全性发布 c# 应用程序会引发 osversion 错误的无效值

c# - 在运行时将绑定(bind)更改为基于列表项的关闭属性的列表的特定元素

data-binding - 自定义控件的 SAPUI5/OpenUI5 数据绑定(bind)

wpf - 使用绑定(bind)自动从填充的组合框中选择一个项目?银光

html - 如何在标题的底部中间添加一个半圆?

python - 值错误: array is not broadcastable to correct shape - Python

c# - 将 XAML 转换为 XBAP

wpf - 在 WPF 中以编程方式更改元素的位置

javascript - 创建平面网格,其中点定义颜色