开发者

Silverlight 4: how to display list of custom controls (not in list order)

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 - co开发者_运维知识库llection 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜