I have a canvas and I would love to bind some points there on specified XY location. However, I can’t achieve this with ItemsControl. I found some solutions but I guess they are not for Windows Phone.
The XAML I’m using is:
<ItemsControl ItemsSource="{Binding Path=Nodes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=XPos}" />
<Setter Property="Canvas.Top" Value="{Binding Path=YPos}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
And I get: “The member “ItemContainerStyle” is not recognized or is not accessible”
How to do that kind of binding in other way? I have name of object, and X/Y values and I want to bind is as a some kind of pushpin
I encountered the same issue but solved it using a TriggerAction instead. You can use the
System.Windows.Interactivityif you have the Blend SDK. The dll is located inThen, by using your previous xaml code I can set the datatemplate as such:
Note the
ia:Interactionprefix, from the interactivity dll mentioned before. You load it within the top of your xaml file.
The tr prefix is for including my own class, which looks like this:
Two things to note with the Invoke method. The first is
AssociatedObject, that gets resolved to the Ellipse since the trigger is nested under it in the xaml. The second thing is the VisualTreeHelper, which gets the parent to the ellipse. This is the ContentPresenter on which we want to set the attached properties of the canvas.It might look like it’s more complicated, but as with everything else in mvvm, you can reuse it in xaml and you don’t have to copy and paste code-behind code everywhere.