Silverlight combobox number of items shown refresh

回眸只為那壹抹淺笑 提交于 2019-12-01 13:25:22

This is a known bug in the ComboBox in Silverlight 2. I think its been fixed in SL 3.

You can fix this by doing the following:

  1. Inherit from the ComboBox

    public class MyComboBox : ComboBox

  2. Get a reference to the "Popup" part of the ComboBox inside the OnApplyTemplate() method

        Popup thePopup = GetTemplateChild("Popup") as Popup;
        FrameworkElement thePopupContent = thePopup.Child as FrameworkElement;
    
  3. Override the OnItemsChanged method

  4. Inside the overridden OnItemsChagned method reset the Height & Width dependency properties on the Popup using the ClearValue(DP) method.

            thePopupContent.ClearValue(FrameworkElement.WidthProperty);
            thePopupContent.ClearValue(FrameworkElement.HeightProperty);
    

You can clear the Max and Min Height & Width properties if you are worried about those too.

That was a perfect solution. Thank you markti.

For those interested the class would look like this:

using System.Windows.Controls.Primitives; 

public class WorkAroundComboBox: ComboBox
{
    FrameworkElement thePopupContent;

    public override void OnApplyTemplate()
    {
        Popup thePopup = GetTemplateChild("Popup") as Popup;
        thePopupContent = thePopup.Child as FrameworkElement;
        base.OnApplyTemplate();
    }

    protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        thePopupContent.ClearValue(FrameworkElement.WidthProperty);
        thePopupContent.ClearValue(FrameworkElement.HeightProperty);
        base.OnItemsChanged(e);
    }
}

}

I think the problem is that Silverlight doesn't fully realize that the data behind ComboBox 2 has changed. Maybe try adding OnPropertyChanged("Group2") to the set for Group1 - that should help Silverlight to realize that it needs to update the bindings for ComboBox 2.

It also might help to call OnPropertyChanged for Group2Selection, since the previous value is no longer valid.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!