1

I want to show multiple image on a canvas. I need to position them differently in the canvas. I made a class for my images :

class MapItem:Image
{
  public int DistanceToTop { get; set; }
  public int DistanceToLeft { get; set; }
}

My XAML looks like this :

<UserControl.DataContext>
    <Map:MapViewModel/>
</UserControl.DataContext>

<ItemsControl ItemsSource="{Binding All}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="Image">
            <Setter Property="Canvas.Left" Value="{Binding DistanceToLeft}" />
            <Setter Property="Canvas.Top" Value="{Binding DistanceToTop}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

My ViewModel used as DataContext :

class MapViewModel : ViewModelBase
{
  public ObservableCollection<MapItem> All   { get; set; }

  public MapViewModel()
  {
    All = new ObservableCollection<MapItem>();
    var wSource = new BitmapImage(new Uri(@"ImagePath"));
    var wImage = new MapItem { Source = wSource, DistanceToLeft = 20, DistanceToTop = 20 };
    test = wImage;
    All.Add(wImage);
  }
}

Why in the XAML my binding to DistanceToLeft and DistanceToTop are not working ?!? Isn't it suppose to automatically look in the object use in my ObservableCollection ?

EDIT : I still have my problem. But now I know it's related with the Binding. I'm trying to implement all this using the MVVM pattern using the GalaSoft framework. So to start I set my DataContext to my MapViewModel. Why can't I acces the properties of the MapItem from my ObservableCollection ?

EDIT : Finally with the help of Clemens and Rachel I ended up with this.

My MapItem Class:

class MapItem:Image
{
  public LatLon CoordMiddleOfImage { get; set; }
  public LatLon CoordTopLeftOfImage { get; set; }

  public int DistanceToTop
  {
    get { return (int) Canvas.GetTop(this); }
    set { Canvas.SetTop(this, value); }
  }

  public int DistanceToLeft
  {
    get { return (int)Canvas.GetLeft(this); }
    set { Canvas.SetLeft(this, value); }
  }

  public int ZOrder
  {
    get { return Panel.GetZIndex(this); }
    set { Panel.SetZIndex(this, value); }
  }
}

And my XAML like this :

<ItemsControl ItemsSource="{Binding All}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas ClipToBounds="True"  SnapsToDevicePixels="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

It works like a charm for now :-)

Joel Bourbonnais
  • 2,618
  • 3
  • 27
  • 44

2 Answers2

5

I don't quite understand why after all you invented the DistanceToLeft and DistanceToTop properties and then struggle with binding. If you want to use Image controls as items, why not directly apply the attached properties Canvas.Left and Canvas.Top:

All = new ObservableCollection<Image>(); // no need for derived MapItem
var wSource = new BitmapImage(new Uri(@"ImagePath")); 
var wImage = new Image { Source = wSource }; 
Canvas.SetLeft(wImage, 20);
Canvas.SetTop(wImage, 20);
All.Add(wImage); 

Hence, no need for a Style:

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

However, you should consider to create a real ViewModel class that is not a control, something like this:

public class ImageItem
{
    public string Source { get; set; }
    public double Left { get; set; }
    public double Top { get; set; }
}

Use it similar to your MapItem class

All = new ObservableCollection<ImageItem>();
ImageItem image = new ImageItem { Source = @"ImagePath", Left = 20, Top = 20 };
All.Add(image);

You would now define an ItemContainerStyle like this:

<ItemsControl.ItemContainerStyle>
    <!-- ContentPresenter is the default item container in ItemsControl -->
    <Style TargetType="ContentPresenter">
        <Setter Property="Canvas.Left" Value="{Binding Left}"/>
        <Setter Property="Canvas.Top" Value="{Binding Top}"/>
        <Setter Property="ContentTemplate">                        
            <Setter.Value>
                <DataTemplate>
                    <Image Source="{Binding Source}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I would like to integrate ZIndex to and a lot of other parameters to my object. Your solution could work but utilmately the best would be to get my other attempt to work. Thanks for your advice but I would really like to get the other way working. – Joel Bourbonnais Aug 24 '12 at 17:48
  • I saw the edit. I got it to work. When I run my app everything is working correctly. But when I look at the XAML I still get errors on my bindings. But it is workin. Really weird ! Thanks alot !!! – Joel Bourbonnais Aug 24 '12 at 18:28
1

An ItemsControl wraps each item in a ContentPresenter, so the style in ItemContainerStyle is for the ContentPresenter, not the Image

If you remove TargetType="Image" from your style it should work fine

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • I already tried that and it still says "Cannot resolve property 'DistanceToLeft' in object of type 'Testing.Map.MapViewModel' wich is directly my dataContext. If I put 20 as the value it works... So it's the binding that is causing the problem.... – Joel Bourbonnais Aug 24 '12 at 16:12
  • @KOTIX That error means it can't find the property on your class. Make sure it is a Property and not a Field (meaning it has `get`/`set` accessors), and that it is a public property on a public class. – Rachel Aug 24 '12 at 16:14
  • As you can see in my class MapItem it is a property with a get/set but it is within my ObservableCollection. Is that a problem ? This property is set for each image I want to print from my Collection so I can't just put it in my viewModel... – Joel Bourbonnais Aug 24 '12 at 16:19
  • @KOTIX I just noticed your error says it cannot resolve the property in object of type `MapViewModel`. Based on the code above, I had assumed you were binding your `ItemsControl` to an `ObservableCollection`, so the binding should be referencing `MapItem.DistanceToLeft`, however the error you have posted is trying to find `MapViewModel.DistanceToLeft`. It looks like you need to double-check your bindings. I'd suggest using a tool like [Snoop](http://snoopwpf.codeplex.com/) for that. – Rachel Aug 24 '12 at 16:38
  • I tried snoop but to be honest I don't know what I'm looking for in there... I can see what's my dataContext at each point in the tree and as soon as I enter the Canvas it's always my viewModel being my dataContext. Should it be my MapItem at some point ? – Joel Bourbonnais Aug 24 '12 at 17:51
  • @KOTIX Snoop should show you a `Canvas` with a bunch of of `` objects as children, and the `DataContext` of each `ContentPresenter` should be of type `MapItem` because of your `ItemsSource` binding to the `All` property. – Rachel Aug 24 '12 at 17:57
  • When I bind to the ObservableCollection "All" and it is a normal class, my contentpresenters' DataContext are good. As soon as All is a list of Image or a class derived from Image, my contentpresenters' DataContext are wrong. Could you help me with that ? – Joel Bourbonnais Aug 24 '12 at 19:15
  • @KOTIX Its hard to tell without seeing your code, but you may have to use a `RelativeSource Self` binding since the item in the `ObservableCollection` is a `UI` element. Personally I would suggest not mixing UI elements with data elements at all. Instead make your `MapItem` a regular class with a Source string property in addition to your Top and Left properties, and making the `ItemTemplate` an `Image` that has the `Source` property bound to whatever the string in your `MapItem` is (you may need a `Converter` to convert the string to an `Image` depending on how your image string is stored) – Rachel Aug 24 '12 at 20:13