Silverlight 3 - Data Binding Position of a rectangle on a canvas

前端 未结 5 2177
囚心锁ツ
囚心锁ツ 2020-12-19 13:43

I am currently trying to bind a collection of objects to a Canvas in Silverlight 3 using an ItemsControl as below:



        
5条回答
  •  我在风中等你
    2020-12-19 14:01

    I realize that this already has an answer accepted, but the way to achieve the initial goal without messing with margins is to create a custom ItemsControl and override the PrepareContainerForItemOverride method. In this method, you set the binding in code.

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

提交回复
热议问题