Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Will you probably <em>could</em> figure out the sizes of all the books before adding them to the list box (possibly by parsing and populating the template XAML in code behind then asking the outer control for its size) this would be a lot of work for not much gain. Can't you just simply select a number of books which will be enough to fill the list box, but not so many to slow the rendering down and turn the scroll bar off? This could still be linked to the size of the list box so that as it grew more items were added, but the performance cost for adding a few extra items should be less that the cost of calculating all the sizes again.</p> <p>Quick example: Let's say that the size of a book with one author is 150 pixels. You could take the size of the listbox and divide it by 125 to get a rough estimate of the number of items which would be in-the-ballpark, but not costly to calculate. What you want to avoid is too few items since that will leave empty space.</p> <hr> <p>Edited in light of your comment.</p> <p>In that case you could add the items to the list box (using the method above to get a close guess), then calculate which the last completely visible item is and then remove the extra items. </p> <p>This extension method will get the last element that is completely shown in a listbox or null if no items are visible:</p> <pre><code>public static class ListBoxExtensions { public static FrameworkElement GetLastItem(this ListBox listBox) { double height = listBox.ActualHeight; double currentHeight = 0; FrameworkElement previous = null; foreach (FrameworkElement item in listBox.Items) { currentHeight += item.ActualHeight; if (currentHeight &gt; height) { return previous; } previous = item; } return previous; } } </code></pre> <p>If the list is made larger then you can add enough items to the collection that you fill in the gap and re-run the process.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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