Silverlight 3 – 数据绑定矩形在canvas上的位置
我目前正在尝试使用ItemsControl将一组对象绑定到Silverlight 3中的Canvas,如下所示:
不幸的是,Canvas.Left上的绑定似乎被忽略了。 根据我在这里学到的东西 ,这似乎是由于项目被放置在内容展示器内而不是我在项目面板中指定的实际canvas。
有没有办法可以使用数据绑定来确定canvas上元素的位置?
我意识到这已经接受了答案,但实现初始目标而不弄乱边距的方法是创建自定义ItemsControl并覆盖PrepareContainerForItemOverride方法。 在此方法中,您在代码中设置绑定。
public class CustomItemsCollection : ItemsControl { protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { FrameworkElement contentitem = element as FrameworkElement; Binding leftBinding = new Binding("Left"); // "Left" is the property path that you want to bind the value to. contentitem.SetBinding(Canvas.LeftProperty, leftBinding); base.PrepareContainerForItemOverride(element, item); } }
您不能在Silverlight中使用ItemsControl.ItemContainerStyle
。 它不存在。 它仅存在于ListBox
本身等几个叶级别类中。
你说得对,在Canvas和Rectangle之间插入了一个ContentPresenter。 一种解决方法是设置左边距而不是Canvas.Left
:
我知道这个问题有点旧,但你可以使用渲染变换 – 我正在做类似的事情;
将以下内容添加到ItemsControl中
无需任何自定义控件