代码之家  ›  专栏  ›  技术社区  ›  Blounty

Silverlight 3-画布上矩形的数据绑定位置

  •  5
  • Blounty  · 技术社区  · 16 年前

    我正在尝试使用下面的项控件将对象集合绑定到Silverlight 3中的画布:

    <ItemsControl x:Name="ctrl" ItemsSource="{Binding myObjectsCollection}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas></Canvas>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Stroke="LightGray" Fill="Black"  StrokeThickness="2" 
                       RadiusX="15" RadiusY="15" Canvas.Left="{Binding XAxis}"
                       Height="25" Width="25">
                </Rectangle>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    不幸的是,画布上的绑定似乎被忽略了。从我所学到的 here 这似乎是由于项目被放置在内容演示者中,而不是我在项目面板中指定的实际画布。

    有没有一种方法可以使用数据绑定来确定元素在画布上的位置?

    5 回复  |  直到 14 年前
        1
  •  7
  •   Tom    16 年前

    我意识到这已经有了一个可以接受的答案,但是实现初始目标而不影响页边空白的方法是创建一个自定义的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);
        }
    
    }
    
        2
  •  2
  •   Samuel Slade    14 年前

    不能使用 ItemsControl.ItemContainerStyle 在Silverlight中。它不存在。它只存在于一些叶级类上,比如 ListBox 本身。

        3
  •  1
  •   Mart    16 年前

    您是对的,在画布和矩形之间插入一个ContentPresenter。 解决方法之一是设置左边距,而不是 Canvas.Left :

    <Rectangle Stroke="LightGray" Fill="Black" StrokeThickness="2" 
          RadiusX="15" RadiusY="15" Height="25" Width="25">
        <Rectangle.Margin>
            <Thickness Left="{Binding XAxis}"/>
        </Rectangle.Margin>
    </Rectangle>
    
        4
  •  1
  •   deepcode.co.uk    15 年前

    我知道这个问题有点老,但是你可以使用渲染转换-我正在做类似的事情;

    <ItemsControl ItemsSource="{Binding Notes}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas Background="Aqua"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Width="{Binding W}" Height="{Binding H}" BorderBrush="Navy" BorderThickness="5" CornerRadius="10">
                    <TextBlock Text="{Binding Text}"/>
                    <Border.RenderTransform>
                        <TransformGroup>
                            <RotateTransform Angle="0"/>
                            <TranslateTransform X="{Binding X}" Y="{Binding Y}"/>
                        </TransformGroup>
                    </Border.RenderTransform>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    
        5
  •  -2
  •   Nat    16 年前

    将以下内容添加到您的项控件中

     <ItemsControl.ItemContainerStyle>
        <Style TargetType="{x:Type ContentPresenter}">
          <Setter Property="Canvas.Left" Value="{Binding XPath=XAxis}"/>
        </Style>
      </ItemsControl.ItemContainerStyle>
    

    不需要任何自定义控件

    推荐文章