Silverlight 4:如何显示自定义控件列表(不按列表顺序) [英] Silverlight 4: how to display list of custom controls (not in list order)

查看:53
本文介绍了Silverlight 4:如何显示自定义控件列表(不按列表顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下对象:


  • 'FieldItem'自定义控件;

  • 'Field' -... XAML对象,其中将包含许多字段项目;

  • FieldItemViewModel-承载要通过 FieldItem自定义控件显示的数据的数据类;
  • $ 'FieldItem'控件的b $ b
  • 位置取决于绑定到控件(X和Y)的数据实体参数;

  • 项-ObservableCollection-包含数据的集合。

  • '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.

问题:我应该将哪种对象放入,以便将我的FieldItems的每个项目都显示在其中画布?

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?

我计划使用ListView ...但是...无法想象如何改变列表视图项的位置...

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

欢迎任何想法!

谢谢。

推荐答案

您可以有一个简单的ItemsControl。
ItemsControl只是一个项目容器。
ItemsPanel应该设置为画布。每个项目的数据模板应为 FieldItem控件。
在您的视图模型中公开一个名为Items的属性,该属性将是item数据的集合。
与此类似的东西:

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没有ItemContainerStyle但您可以在代码中设置它:

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);
        }
    }

来自此处: http://forums.silverlight.net/forums/p/29753/96429.aspx

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

这篇关于Silverlight 4:如何显示自定义控件列表(不按列表顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆