c# - 现有 Canvas 中的 ItemsControl

标签 c# xaml itemscontrol windows-8

我正在努力完成以下任务。

1) 在我的页面上有一个 Canvas 控件,带有可缩放和平移的背景图像。这适用于我所拥有的。 2) 在 Canvas 中显示一组控件。这被设想为类似于 Bing 主页在此处显示的图像上显示热点的方式。当图像被平移/缩放时,控件需要在 Canvas 上四处移动。

我试过以下方法

    <Canvas Name="PanoCanvas" >
        <Canvas.Background>
            <ImageBrush x:Name="PanoImage" ImageSource="/Images/OtherImages/PanoBackound.jpg" Stretch="None" />
        </Canvas.Background>
        <ItemsControl Name="POIPresenter">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <uc:PointOfInterest />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Top" Value="{Binding TopPosition}" />
                    <Setter Property="Canvas.Left" Value="{Binding LeftPosition}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
    </Canvas>

并在后面的代码中设置 POIPresenter 的 ItemsSource,但用户控件在左侧(0,0 处)显示在另一个之上。

我曾尝试在 ItemsPanelTemplate 中移动 Canvas 声明,但随后我设置图像比例/中心的声明不起作用 - 它们找不到 PanoCanvas 控件。

有人能指出我在这里绞尽脑汁的明显痛苦吗?

提前致谢。

最佳答案

对于初学者,您可以简化如下:

<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas>
                <Canvas.Background>...</Canvas.Background>
            </Canvas>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

也就是说,使用一个 Canvas 而不是两个。

除此之外,在输出窗口中查找绑定(bind)错误。可能 TopPositionLeftPosition 属性没有被正确解析。

关于c# - 现有 Canvas 中的 ItemsControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8122775/

相关文章:

c# - 主页面的SetContentView

c# - Linq 中的单元测试虚拟方法

c# - 将数据绑定(bind)到 WPF 用户控件

wpf - 找不到WPF MvxEventToCommand

c# - 根据堆栈面板中的选定项目滚动

c# - ItemsControl 拖放

c# - CS8019 临时文件 MSBuild 服务器上的 Assemblyinfo 错误

c# - WCF 发送大文件

c# - 在 WPF 中的 xaml View 中打开另一个窗口

带有交替 ItemTemplate 的 Silverlight ItemsControl