1

I'm working on a graph program that shows different datas in wpf. I want to bind Canvas Drawings to vertices. I have nearly the same problem like in Binding WPF Canvas Children to an ObservableCollection .

(first question): I want to Convert my Path Data (see XAML) into a Geometry. How?!

(second question): I want to Bind theese Paths to "Something" (ItemsControl) and show them.

I can show Canvas data by writing it directly into XAML:

              <Viewbox>

                    <Grid
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left">
                        <Grid.RenderTransform>
                            <TransformGroup>
                            <ScaleTransform ScaleX="8.0" ScaleY="8.0"></ScaleTransform>
                            <TranslateTransform X="-50" Y="0"/>
                                </TransformGroup>
                        </Grid.RenderTransform>

                        <Path
                           Data="M 8.8733,6.4011 C 8.8733,5.3196 7.9235,4.4428 6.7518,4.4428 C 5.5802,4.4428 4.6304,5.3196 4.6304,6.4011 C 4.6304,7.4826 5.5802,8.3594 6.7518,8.3594 C 7.9235,8.3594 8.8733,7.4826 8.8733,6.4011 Z ">
                            <Path.Fill>
                                <SolidColorBrush Color="#FFFFFFFF"/>
                            </Path.Fill>
                        </Path>
                        <Path
                          Data="M 8.8733,6.4011 C 8.8733,5.3196 7.9235,4.4428 6.7518,4.4428 C 5.5802,4.4428 4.6304,5.3196 4.6304,6.4011 C 4.6304,7.4826 5.5802,8.3594 6.7518,8.3594 C 7.9235,8.3594 8.8733,7.4826 8.8733,6.4011 Z "
                          Stroke="#FF000000" StrokeThickness="0.72" StrokeStartLineCap="Round"
                          StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeLineJoin="Round">
                        </Path>

                    </Grid>
                </Viewbox>

But I want to change it according to vertex types.

                        <ItemsControl ItemsSource="{Binding CanvasVecPicture}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Canvas />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>

My Vertex class:

        ...
        private ObservableCollection<Shape> canvasVecPicture;
        public ObservableCollection<Shape> CanvasVecPicture { get { return canvasVecPicture; } }
        ...


        Path myPath = new Path();
        myPath.Stroke = System.Windows.Media.Brushes.Black;
        myPath.Fill = System.Windows.Media.Brushes.MediumSlateBlue;
        myPath.StrokeThickness = 100;
        myPath.HorizontalAlignment = HorizontalAlignment.Left;
        myPath.VerticalAlignment = VerticalAlignment.Center;
        EllipseGeometry myEllipseGeometry = new EllipseGeometry();

        myEllipseGeometry.Center = new System.Windows.Point(0, 0);
        myEllipseGeometry.RadiusX = 2000;
        myEllipseGeometry.RadiusY = 2000;
        myPath.Data = myEllipseGeometry;

        Ellipse ellipse = new Ellipse();

        SolidColorBrush brush = new SolidColorBrush(Brushes.Blue.Color);
        ellipse.Fill = brush;
        ellipse.StrokeThickness = 2;
        ellipse.Stroke = Brushes.Black;

        ellipse.Width = 100;
        ellipse.Height = 100;




        canvasVecPicture = new ObservableCollection<Shape>();
        canvasVecPicture.Add(myPath);
        canvasVecPicture.Add(ellipse);

In debugger the "get" method called lots of time, so I don't think it is a Binding Problem.

Community
  • 1
  • 1
Balázs Szántó
  • 189
  • 3
  • 15
  • Where are you initializing the collection `canvasVecPicture`. I mean in constructor or where? Also check for binding errors in output window. Getter gets hit even in case you access it from code. – Rohit Vats Aug 13 '14 at 08:14
  • 1
    [This answer](http://stackoverflow.com/a/25031206/1136211) might be helpful. – Clemens Aug 13 '14 at 08:15
  • canvasVecPicture = new ObservableCollection(); Last code snippet, and in ctor of the vertex class – Balázs Szántó Aug 13 '14 at 08:20
  • I copy/paste your code in small sample and it does work. Most likely a binding failure. See in output window of Visual Studio. Do you see any binding errors over there? – Rohit Vats Aug 13 '14 at 08:22
  • I checked the output window, and no errors. Then I renamed the binding to check if it shows error, and the modified(wrong) version showed. – Balázs Szántó Aug 13 '14 at 08:30
  • Can you post small sample working code replicating your problem? – Rohit Vats Aug 13 '14 at 08:33
  • Yes give me some minutes :). – Balázs Szántó Aug 13 '14 at 08:34
  • http://www.speedyshare.com/GanvM/GraphSharpDemo.rar In MainWindow.xaml, comment/uncomment static/binding parts – Balázs Szántó Aug 13 '14 at 08:44
  • The problem was with the ... I removed it and solved my problem. One question left ^^... From Data="M 8.8733,6.4011 C 8.8733,5.3196 ... Z " How Can I get Geometry object by using theese datas. – Balázs Szántó Aug 13 '14 at 09:13
  • 1
    Refer to link post by Clemens. It will answer your query. – Rohit Vats Aug 13 '14 at 09:17
  • Ah there is a link actually, I see, I thougth he wrote it to your answer :) – Balázs Szántó Aug 13 '14 at 09:20
  • Also this(How to convert from M 400,600 C 390,575 360,550 400,540 C 440,550 410,575 400,600 to Geometry) http://social.msdn.microsoft.com/Forums/windowsapps/en-US/1e4508c9-7ca5-47c0-a410-a05bf7c3a1f7/convert-geometry-pathdata-string-to-geometry?forum=winappswithcsharp – Balázs Szántó Aug 13 '14 at 09:38

1 Answers1

0

I answer my question, but also I say thanks to Clemens & Rohit Vats.

(first question): I want to Convert my Path Data (see XAML) into a Geometry. How?!

Microsoft Soultion:

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/1e4508c9-7ca5-47c0-a410-a05bf7c3a1f7/convert-geometry-pathdata-string-to-geometry?forum=winappswithcsharp

My Solution:

var path = new Path();
myPath.Stroke = System.Windows.Media.Brushes.Black;
myPath.Fill = System.Windows.Media.Brushes.MediumSlateBlue;
myPath.StrokeThickness = 100;
myPath.HorizontalAlignment = HorizontalAlignment.Left;
myPath.VerticalAlignment = VerticalAlignment.Center;

**path.Data = Geometry.Parse(M 400,600 C 390,...(DATAS);**

(second question): I want to Bind theese Paths to "Something" (ItemsControl) and show them. http://www.speedyshare.com/GanvM/GraphSharpDemo.rar

This is the "problematic" program, but you can get it from my question also. The solution was to remove the < Viewbox > /< Viewbox > lines

Balázs Szántó
  • 189
  • 3
  • 15