Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get a ScrollViewer with a Rectangle inside to stop scrolling when it reaches the end of the rectangle?
    primarykey
    data
    text
    <p>I have created a Rectangle inside of a ScrollViewer like this</p> <pre><code>&lt;ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Center" Width="728" Canvas.Top="20" d:LayoutOverrides="HorizontalMargin" &gt; &lt;Rectangle x:Name="musicBG" Fill="#FF0692FD"/&gt; &lt;/ScrollViewer&gt; </code></pre> <p>During the use of the app, the size of MusicBg changes, sometimes to something around 3,000 pixels width.</p> <pre><code>musicBG.Width = _songLength*PixelsPerSecond </code></pre> <p>However, while scrolling the scrollViewer, it allows me to scroll the rectangle all the way off the screen.</p> <p>For example this line of code gives me the following values when I have moved the rectangle as far as I want to move it.</p> <pre><code>if (songScrollViewer.HorizontalOffset &gt; songScrollViewer.ScrollableWidth) </code></pre> <p>HorizontalOffset has a value of ~1200 and ScrollableWidth has a value of about ~2900.</p> <p>How can I get this to be done properly so that the rectangle is not scrolled completely off the screen?</p> <p>I would expect a HorizontalOffset of about 1200 to only push the rectangle about halfway through to it's destination, and not make it start going off screen.</p> <p><strong>ANSWER:</strong></p> <p><strong>After much frustration, I was able to solve this problem by using Canvas instead of Border or Rectangle. I'll award points if anyone can explain why this problem happened, and if there is a less processor intensive control that would work better than canvas.</strong></p> <p>Edit: Screen shots:</p> <p>Bad Code:</p> <pre><code>&lt;ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" Width="720" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Top" Canvas.Top="20" HorizontalAlignment="Left" &gt; &lt;Border x:Name="musicBG" Background="#FF0692FD" VerticalAlignment="Top" HorizontalAlignment="Left" Height="270" /&gt; &lt;/ScrollViewer&gt; </code></pre> <p>Image of bad scroll with bad code: <img src="https://i.stack.imgur.com/0jNc4.png" alt="bad scroll"></p> <p>Good working code:</p> <pre><code>&lt;ScrollViewer ManipulationMode="Control" x:Name="songScrollViewer" Width="720" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Height="270" VerticalAlignment="Top" Canvas.Top="20" HorizontalAlignment="Left" &gt; &lt;Canvas x:Name="musicBG" Background ="#FF0692FD" Height="270" &gt; &lt;Border Background="#FF0692FD" VerticalAlignment="Top" HorizontalAlignment="Left" Height="270" /&gt; &lt;/Canvas&gt; &lt;/ScrollViewer&gt; </code></pre> <p>Good Scroll: Notice it says 170 seconds on the bottom right instead of the smaller number of 118 seconds in the bad scroll. <img src="https://i.stack.imgur.com/kUZ2c.png" alt="good scroll"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload