WPF Combobox Popup Width

Jan 16, 2009 at 11:16 AM
Hi Guys,

I get a combobx popup in code behind as 

_popup = MyComboBox.Template.FindName(

"PART_Popup", MyComboBox)

I want the popup to appear at the mouse pointer when user clicks the combo toggle button  so I change the _popup.Placement from PlacementMode.Bottom to PlacementMode.MousePoint. This works.

 

 

But the popup that appears is always of the same width sa that of the parent combobox. I need popup to not expand to the size of the combob but adjust to its underlying contents (a list of names) width.

How would I do that?

Setting _popup.Width = 80 works for me... but It has 2 problems ...

1. I shouldnt use absolute pixel widths.
2. The border of the popup clips on the right side because actual width of the combox (which is also the width of the popup) can vary and is mostly greater than 80. 


Can anyone tell me a sophisticated way to auto adjust width of the popup to its content size?
 

Thx.
Vinit.

Coordinator
Jan 16, 2009 at 7:43 PM
Popup is a primitive which does not have a full layout stack inside.  So unfortunately there is no easy way to have the popup automatically re-size.
If you know the width of the content of the popup, which you can get from ActualWidth of the content if it's been displayed before, you can set the popup's width each time it opens.
Jan 16, 2009 at 7:54 PM
Hi elclark,

Thx for replying.... I did set the ActualWidth of the underlying contents but that always returns 0.0 So I am not sure what to do.

As of now, because my given application only displays the list of names, I assigned a ListBox (which binds itself to the combobox's ItemsSource,  DisplayMemeberPath, SelectedValuePath and SelectedValue and SelectedItem) as _popup.Child.And it worked.

The listbox only fitted to the width of the list and not to that of the combo.

So as of now I have a workaround.

Thx for the help!

:-)