How to implement IPathLayoutItem interface in Silverlight? RRS feed

  • Question

  • Being involed in an interesting forum question about how to design a dynamic path, I came up with the idea of user created dynamic paths at runtime which are assigned at runtime as SourceElemt to LayoutPath objects, which are assigned to the LayoutPaths collection of a PathListBox. I created a sample application, contributed here at the Expression Gallery, which is a kind of intermediate experimental result.

    When you check the sample app, you will notice, that the items which are assigned to the item collection of the PathListBox at runtime are added on the MouseLeftButtonUp event. What I would like to achieve is to add the items in the MouseMove event frequently and depending on the length of the path and the position of the most recently added item.

    On my search for a solution of this task I found the IPathLayoutItem interface. The IPathLayoutItem interface has a property called LocalOffset, which seems to provide the data I need to solve my task.

    Unfortunately there is no in depth documentation on the IPathLayoutItem interface. So, could anyone provide a sample on how to use and implement this interface?

    Any help is very appreciated.

    Tuesday, March 22, 2011 10:24 AM

All replies

  • I am testing some code and what I found out is this.

    I can get readonly access to the LocalOffset value of a PathListBoxItem with a code snippet like this, which is casting a PathListBoxItem to an IPathLayoutItem:

      Dim pldGet As New PathLayoutData
      pldGet.LocalOffset = CType(dynamicPathListBox.Items.ElementAt(3), IPathLayoutItem).LocalOffset

    To set a new value for LocalOffset I was trying the following code:

     Private Sub MakeNewLayoutPath()
      Dim layoutPath As New LayoutPath With {.SourceElement = pathToDraw}
      For i As Integer = 0 To 5
       dynamicPathListBox.Items.Add(New PathListBoxItem With {.Width = 20,
                                   .Height = 20,
                                   .Background = New SolidColorBrush(Colors.Red)})
      Canvas.SetZIndex(pathToDraw, -1)
      layoutPath.Capacity = 7
      layoutPath.Distribution = Distribution.Padded
      ' Get current LocalOffset value of 4th item in line:
      Dim pldGet As New PathLayoutData
      pldGet.LocalOffset = CType(dynamicPathListBox.Items.ElementAt(3), IPathLayoutItem).LocalOffset
      ' >>> Resulting value: 0.0
      ' Set new LocalOffset value of 4th item in line:
      Dim pldSet As New PathLayoutData With {.LocalOffset = 0.8}
      CType(dynamicPathListBox.Items.ElementAt(3), IPathLayoutItem).Update(pldSet)
      ' >>> Resulting value: 0.8 but no visual affection
     End Sub

    The routine MakeNewLayoutPath is called from MouseLeftButtonUp event. It takes a path (pathToDraw, which was created in code behind before) and assignes this path to the SourceElement property of a new LayoutPath object. Then, 6 PathListBoxItems are added to the items collection of the PathListBox. Then, I read the LocalOffset value of the 4th item in line. And what confuses me is that the return value is 0.0, although it is the 4th value in line.

    Next, I set a new value for LocalOffset for the 4th item in line. Therefore I create a new PathLayputData object (named "pldSet") and set it's LocalOffset value to 0.8. Then i call Update() method on the 4th item in line and pass pldSet as parameter. The resulting return value is actually 0.8 when I look at the value while debugging. But there is no visually affection on the UI. The 4th item in line doesn't change it's position.

    I tried alternatively to change the LocalOffset of the 4th item in line in a Button_click event handler with the following code snippet:

     Private Sub b1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles b1.Click
      ' Set new LocalOffset value for 4th item in line:
      Dim pld As New PathLayoutData With {.LocalOffset = 0.4}
      AddHandler CType(dynamicPathListBox.Items.ElementAt(3), IPathLayoutItem).PathLayoutUpdated, AddressOf TestUpDate
      CType(dynamicPathListBox.Items.ElementAt(3), IPathLayoutItem).Update(pld)
      ' >>> Resulting value: 0.4 and the 4th item in line disappears from the UI
     End Sub

    The resulting value after setting the new value is 0.4. Visually, on the UI, the 4th item in line disappears.

    So, this is quite confusing to me and I don't know what I am doing wrong.

    It would be very cool if someone would point into the right direction to solve this.


    Tuesday, March 22, 2011 5:20 PM