Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Databinding on combobox - layout update RSS

22 replies

Last post Aug 26, 2010 04:05 AM by JoseHidalgo

(0)
  • meykih

    meykih

    Participant

    1049 Points

    260 Posts

    Databinding on combobox - layout update

    Nov 11, 2008 11:40 AM | LINK

    I have a little problem with the combobox control: The binding of a list works absolutely well even if I change the list at runtime. My problem is that the length of the dropdown part does not change anymore after opening it for the first time. An example:

    When the page is loaded the combobox contains four items. When I open it I see them all. After that I for example press a button and now the combobox contains seven items. But when I open it it I still see only the first four items and a scrollbar for the rest...

    I already tried UpdateLayout but that didn't do it. Does anyone have an idea how to change the listlength at runtime everytime the number of items changes?

    Thanks
    Regards,
    Maike Ohlig

    Please mark post as answer if it helped you
  • Ken Tucker

    Ken Tucker

    All-Star

    23232 Points

    3530 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 12:05 PM | LINK

    Set the combobox's MaxDropDownHeight

     

     

                    <ComboBox x:Name="searchBox" Margin="10,10,10,10" Grid.Column="1" Grid.Row="0" Width="300" MaxDropDownHeight="150" >
    
     

  • meykih

    meykih

    Participant

    1049 Points

    260 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 12:13 PM | LINK

     Doesn't work. Still no size update after the first opening...

    Regards,
    Maike Ohlig

    Please mark post as answer if it helped you
  • IanBlackburn

    IanBlackburn

    Contributor

    2333 Points

    371 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 12:20 PM | LINK

    ken tucker

    Set the combobox's MaxDropDownHeight

     

    Hi Ken,

    Does that work for this scenario?  It restricts the height, but only upto a maximum, and the initial height is set during the first binding.

    Here we want the opposite - we want to expand the height of the DropDownList to fit all the added items in without using a scrollbar.  MinDropDownHeight would seem to be the anser, but there is no such property!

    I got this working by creating the whole thing in code like this, but it's not very satifactory:

        public partial class Page : UserControl
        {
            ObservableCollection people;
            ComboBox combo1;
            public Page()
            {
                InitializeComponent();
                people = new ObservableCollection();
                people.Add(new Person() { Id = 1, Name = "Fred" });
                people.Add(new Person() { Id = 2, Name = "John" });
                people.Add(new Person() { Id = 3, Name = "Paul" });
                people.Add(new Person() { Id = 4, Name = "Jane" });
                people.Add(new Person() { Id = 5, Name = "Julie" });
                CreateCombo();
                
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                people.Add(new Person() { Id = 6, Name = "Eileen" });
                LayoutRoot.Children.Remove(combo1);
                CreateCombo();
            }
            private void CreateCombo()
            {
                combo1 = new ComboBox();
                combo1.ItemsSource = people;
                combo1.DisplayMemberPath = "Name";
                LayoutRoot.Children.Add(combo1);
            }
    
    
        }
     

    The combobox must be doing some work somwhere where it works out this height - we need to find out how to get it to do it again!

    I'll have a look in reflector and see what I can find...

    (If this has answered your question, "Mark as Answer" - many thanks)

    Cheers

    Ian Blackburn
    SilverlightForBusiness.net
  • IanBlackburn

    IanBlackburn

    Contributor

    2333 Points

    371 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 12:37 PM | LINK

    Ok here is the answer:

     

    people.Add(new Person() { Id = 6, Name = "Eileen" });
                Rect r = new Rect();
                //rough calcuation follows!!
                r.Height = people.Count * 25;
                r.Width= combo1.ActualWidth;
                Combo2.Arrange(r);

     The only thing left to do is work out the height of each row so you can calculate the correct Rect size. 

    Hope this helps.

    (If this has answered your question, "Mark as Answer" - many thanks)

    Cheers

    Ian Blackburn
    SilverlightForBusiness.net
  • meykih

    meykih

    Participant

    1049 Points

    260 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 12:47 PM | LINK

     I'm really sorry but my combodropdown still keeps its length set at first opening...

    Regards,
    Maike Ohlig

    Please mark post as answer if it helped you
  • IanBlackburn

    IanBlackburn

    Contributor

    2333 Points

    371 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 01:15 PM | LINK

    meykih

    I'm really sorry but my combodropdown still keeps its length set at first opening...

     

    Can you post the xaml and the code you are using so I can replicate it.

    (If this has answered your question, "Mark as Answer" - many thanks)

    Cheers

    Ian Blackburn
    SilverlightForBusiness.net
  • meykih

    meykih

    Participant

    1049 Points

    260 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 01:38 PM | LINK

    IanBlackburn

    Can you post the xaml and the code you are using so I can replicate it.

     

     

    Hi Ian,

    unfortunately it's code written for my employer... And too complex to put it into a small anonymous application. So, I think I have to go on trying by myself, yes?

    Regards,
    Maike Ohlig

    Please mark post as answer if it helped you
  • IanBlackburn

    IanBlackburn

    Contributor

    2333 Points

    371 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 01:45 PM | LINK

    Did you try increasing the value here:

     

    r.Height = people.Count * 50;
     

    (If this has answered your question, "Mark as Answer" - many thanks)

    Cheers

    Ian Blackburn
    SilverlightForBusiness.net
  • meykih

    meykih

    Participant

    1049 Points

    260 Posts

    Re: Databinding on combobox - layout update

    Nov 11, 2008 01:50 PM | LINK

    IanBlackburn

    Did you try increasing the value here:

     

    r.Height = people.Count * 50;

     

     

     

    I even tried a factor 100 and nothing happend. Also the other case that the second list is shorter than the first one makes no changes to the size. In that case I see some empty rows below the actual items. Absolutely no size update...

    Regards,
    Maike Ohlig

    Please mark post as answer if it helped you