如何使用 Canvas 作为 ItemsPaneltemplate 在项目面板中显示滚动条 [英] How to Visible ScrollBar In Items Panel With Canvas as ItemsPaneltemplate

查看:36
本文介绍了如何使用 Canvas 作为 ItemsPaneltemplate 在项目面板中显示滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将 ItemsControl 与 Canvas 作为 ItemsPanelTemplate.itempanel source的items是thumb对象,可以拖拽.如果项目退出屏幕视图,我无法滚动查看它们.我想怎么做?我的代码如下:

In My Application I Have ItemsControl with Canvas as ItemsPanelTemplate. Items of itemspanel source is thumb object and can drag and drage. if items drop out of screen view , I dont can scroll to view them. How I Want To Do? My Code Is Below :

            <ItemsControl ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Canvas.Left" Value="{Binding XPosition}"/>
                        <Setter Property="Canvas.Top" Value="{Binding YPosition}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

推荐答案

首先,你需要用一个 ScrollViewer 包围 ItemsControl(不像 ListBox,它有它在其控制模板中):

First of all, you need to surround the ItemsControl with a ScrollViewer (unlike ListBox, which has it in its control template):

<ScrollViewer>
    <ItemsControl> ... </ItemsControl>
<ScrollViewer>

其次,Canvas 不会在其子项之后调整自身大小.所以你有几个选择:

Secondly, a Canvas does not resize itself after its children. So you have a few options:

  • 使用带有单个单元格的网格并使用 Margin 属性设置位置.
  • 计算最大尺寸并设置 Canvas 的 WidthHeight 属性.
  • 从 Canvas 继承并覆盖 MeasureOverride 方法,以便它们反映子项的大小.(请参阅此答案.)
  • Use a Grid with a single cell and set the positions using the Margin property.
  • Calculate the maximum size and set the Canvas' Width and Height properties.
  • Inherit from Canvas and override the MeasureOverride method so they reflect the children's sizes. (See this answer.)

这篇关于如何使用 Canvas 作为 ItemsPaneltemplate 在项目面板中显示滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆