views:

29

answers:

1

There are following object:

  • 'FieldItem' custom control;
  • 'Field' - ... XAML-object, which will contains a dozen of field items;
  • FieldItemViewModel - data class that hosts data to be displayed with 'FieldItem' custom control;
  • position of 'FieldItem' control depend from data entity parameters that is bounded to the control (X and Y);
  • items - ObservableCollection - collection that contains a data.

Question: what kind of object should I put inside of the in order to have each item of the my FieldItems to be displayed inside of Canvas?

I've planned to use ListView... but... can't imagine how is it possible to change position of the list view item...

Any thoughts are welcome!

Thanks.

+1  A: 

You can have a simple ItemsControl. ItemsControl is just a container of items. The ItemsPanel should be set to your canvas. And the data template of each item should be the 'FieldItem' control. In your viewmodel expose a property that is called Items which will be a collection of the items data. Something similar to this:

<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <FieldItem  />
    </DataTemplate>
</ItemsControl.ItemTemplate>
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

Silverlight doesn't have ItemContainerStyle but you can set it in code:

   public class MyItemsControl : ItemsControl
    {
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            FrameworkElement contentitem = element as FrameworkElement;
            Binding leftBinding = new Binding("Position.X");
            Binding topBinding = new Binding("Position.Y");
            contentitem.SetBinding(Canvas.LeftProperty, leftBinding);
            contentitem.SetBinding(Canvas.TopProperty, topBinding);
            base.PrepareContainerForItemOverride(element, item);
        }
    }

Taken from here: http://forums.silverlight.net/forums/p/29753/96429.aspx

Yuval Peled
Yuval, could you please also clarify: which property of my 'FieldItemViewModel' class should contain 'X' and 'Y' values? Should that be a plain 'X' and 'Y' properties or I need to have 'Position' property which itself has 'X' and 'Y'? Thank you in advance.
Budda
Yuval, thanks... Now I see, that "Position.X/Y" properties are looked inside of object that is bound to an item. Thanks!
Budda