itsource

WPF C# 패스: 패스 데이터가 있는 문자열에서 코드(XAML 이외)로 지오메트리로 이동하는 방법

mycopycode 2023. 4. 22. 09:27
반응형

WPF C# 패스: 패스 데이터가 있는 문자열에서 코드(XAML 이외)로 지오메트리로 이동하는 방법

Code에서 WPF Path 개체를 생성합니다.

XAML에서는 다음을 수행할 수 있습니다.

 <Path Data="M 100,200 C 100,25 400,350 400,175 H 280">

어떻게 하면 Code에서도 같은 일을 할 수 있을까요?

 Path path = new Path();
 Path.Data = "foo"; //This won't accept a string as path data.

PathData를 사용하여 문자열을 PathGeometry로 변환하는 클래스/메서드가 있습니까?

XAML이 해석되고 데이터 문자열이 변환되는 것은 확실합니까?

var path = new Path();
path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 400,175 H 280");

Path.Data는 지오메트리 유형입니다.사용. 리플렉터 JustDecompile(eff Red Gate)에서 TypeConverterAttribute(xaml 시리얼라이저가 타입의 값을 변환하기 위해 사용하는)의 지오메트리의 정의를 확인했습니다.string로.GeometryGeometry Converter가 표시되었습니다.실장을 확인해보니 이 제품은Geometry.Parse경로의 문자열 값을 지오메트리 인스턴스로 변환합니다.

바인딩 메커니즘을 사용할 수 있습니다.

var b = new Binding
{
   Source = "M 100,200 C 100,25 400,350 400,175 H 280"
};
BindingOperations.SetBinding(path, Path.DataProperty, b);

도움이 됐으면 좋겠네요.

원래 텍스트 문자열에서 형상을 만들려면 시스템을 사용할 수 있습니다.창문들.Media. 포맷 완료BuildGeometry() 메서드를 사용한 텍스트 클래스

 public  string Text2Path()
    {
        FormattedText formattedText = new System.Windows.Media.FormattedText("Any text you like",
            CultureInfo.GetCultureInfo("en-us"),
              FlowDirection.LeftToRight,
               new Typeface(
                    new FontFamily(),
                    FontStyles.Italic,
                    FontWeights.Bold,
                    FontStretches.Normal),
                    16, Brushes.Black);

        Geometry geometry = formattedText.BuildGeometry(new Point(0, 0));

        System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
        path.Data = geometry;

        string geometryAsString = geometry.GetFlattenedPathGeometry().ToString().Replace(",",".").Replace(";",",");
        return geometryAsString;
    }

언급URL : https://stackoverflow.com/questions/2029680/wpf-c-sharp-path-how-to-get-from-a-string-with-path-data-to-geometry-in-code-n

반응형