具有嵌套ItemControls的ItemContainer样式无法正确绑定

时间:2015-04-25 20:01:50

标签: c# wpf binding

我想要做的事情摘要:

  1. 我有一个形状列表
  2. 所有形状都有一个块列表
  3. 我想在画布中绘制所有块并绑定它们的位置。
  4. 但现在的问题是,即使它画了我的块,它也不会正确绑定位置,它们总是显示在(0,0)。

    以下是嵌套ItemsControl的现状:

    <ItemsControl ItemsSource="{Binding Path=Shapes}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas Background="Aqua" Width="250" Height="400"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding Blocks}">
                        <ItemsControl.ItemContainerStyle>
                            <Style TargetType="ContentPresenter">
                                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                                <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="Black" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    

    那么我做错了什么或者做嵌套的ItemControls是不是很糟糕?

1 个答案:

答案 0 :(得分:0)

尝试使用RenderTransform而不是Canvas Left和Top

FrameLayout.LayoutParams
相关问题