mehdi lotfi
mehdi lotfi

Reputation: 11571

How to Visible ScrollBar In Items Panel With Canvas as ItemsPaneltemplate

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>

Upvotes: 1

Views: 1867

Answers (2)

Vincent
Vincent

Reputation: 3304

I solved by setting/updating Canvas's Height.

Upvotes: 0

Eli Arbel
Eli Arbel

Reputation: 22739

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>

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

  • 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.)

Upvotes: 2

Related Questions