Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:02:29+00:00 2026-05-30T22:02:29+00:00

Is there some way to set the Height attribute of a WPF multi-select ListBox

  • 0

Is there some way to set the Height attribute of a WPF multi-select ListBox to be a multiple of the item height, similar to setting the size attribute of an html select element?

I have a business requirement to not have half an item showing at the bottom of the list (if it’s a long list with a scrollbar), and not have extra white space at the bottom (if it’s a short list with all items showing), but the only method I can find to do this is to just keep tweaking the Height until it looks about right.

(What else have I tried? I’ve asked colleagues, searched MSDN and StackOverflow, done some general Googling, and looked at what VS Intellisense offered as I edited the code. There’s plenty of advice out there about how to set the height to fit the ListBox’s container, but that’s the opposite of what I’m trying to do.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:02:30+00:00Added an answer on May 30, 2026 at 10:02 pm

    Yeah, one could imagine there would be an easier way to do it (a single snapToWholeElement property). I couldn’t find this property as well.

    To achieve your requirement, I’ve wrote a little logic. Basically, In my Windows object I’ve a public property lbHeight which is calculate the listbox height by calculating the height of each individual item.

    First, let’s take a look at the XAML:

    <Window
        x:Class="SO.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="120" SizeToContent="Height"
        Title="SO Sample"    
        >
        <StackPanel>
            <ListBox x:Name="x_list" Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=lbHeight}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border x:Name="x" Background="Gray" Margin="4" Padding="3">
                            <TextBlock Text="{Binding}" />
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>        
    </Window>
    

    Note that the ItemTemplate is somewhat non trivial. One important thing to notice is that I gave this item a Name – so I can find it later.

    In the code-behind constructor I put some data in the list box:

    public MainWindow( )
    {
        InitializeComponent( );
        this.x_list.ItemsSource = Enumerable.Range( 0, 100 );
    }
    

    next, I’m implementing a findVisualItem – to find the root element of the data template. I’ve made this function a little generic, so it get a predicate p which identify whether this is the element I want to find:

    private DependencyObject findVisualItem( DependencyObject el, Predicate<DependencyObject> p )
    {
        DependencyObject found = null;
    
        if( p(el) ) {
            found = el;
        }
        else {
            int count = VisualTreeHelper.GetChildrenCount( el );
            for( int i=0; i<count; ++i ) {
                DependencyObject c = VisualTreeHelper.GetChild( el, i );
                found = findVisualItem( c, p );
                if( found != null )
                    break;
            }
        }
        return found;
    }
    

    I’ll use the following predicate, which returns true if the element I’m looking for is a border, and its name is “x”. You should modify this predicate to match your root element of your ItemTemplate.

    findVisualItem(
        x_list,
        el => { return ( el is Border ) ? ( (FrameworkElement)el ).Name == "x" : false; }
        );
    

    Finally, the lbHeight property:

    public double lbHeight
    {
        get {
            FrameworkElement item = findVisualItem( 
                x_list,
                el => { return ( el is Border ) ? ( (FrameworkElement)el ).Name == "x" : false; }
                ) as FrameworkElement;
            if( item != null ) {
                double h = item.ActualHeight + item.Margin.Top + item.Margin.Bottom;
                return h * 12;
            }
            else {
                return 120;
            }
        }
    }
    

    I’ve also made the Window implementing INotifyPropertyChanged, and when the items of the list box were loaded (Loaded event of ListBox) I fired a PropertyChanged event for the ‘lbHeight’ property. At some point it was necessary, but at the end WPF fetched the lbHeight property when I already have a rendered Item.

    It is possible your Items aren’t identical in Height, in which case you’ll have to sum all the Items in the VirtualizedStackPanel. If you have a Horizontal scroll bar, you’ll have to consider it for the total height of course. But this is the overall idea. It is only 3 hours since you published your question – I hope someone will come with a simpler answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some way to keep a set width/height for a DIV, and pad
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
Is there some way to block access from a referrer using a .htaccess file
Is there some way to hide the browser toolbar / statusbar etc in current
Is there some way to cache a generated report in the reporting services, so
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Is there some way to get a value from the last inserted row? I
Is there some way to catch exceptions which are otherwise unhandled (including those thrown
Is there some way to have the Mail Merge capability in my application? Maybe
Is there some way to determine what file currently is being rendered by Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.