@@ -98,8 +98,13 @@ private static void OnDropDownOpened(object sender, object args)
9898
9999 private static void AlignSelectedContainer ( ComboBox comboBox , Popup popup , bool isOpenDown )
100100 {
101- if ( ! isOpenDown ||
102- GetToAlignContainer ( comboBox ) is not { } itemContainer ||
101+ if ( comboBox . IsEditable )
102+ {
103+ popup . VerticalOffset = 0 ;
104+ return ;
105+ }
106+
107+ if ( GetToAlignContainer ( comboBox , isOpenDown ) is not { } itemContainer ||
103108 itemContainer . TranslatePoint ( new Point ( 0 , - itemContainer . ActualHeight + comboBox . Padding . Top ) ,
104109 comboBox ) is not { Y : not 0 } itemTop )
105110 {
@@ -114,27 +119,20 @@ private static void AlignSelectedContainer(ComboBox comboBox, Popup popup, bool
114119 }
115120 }
116121
117- private static FrameworkElement GetToAlignContainer ( ComboBox comboBox )
122+ private static FrameworkElement GetToAlignContainer ( ComboBox comboBox , bool isOpenDown )
118123 {
119124 DependencyObject container ;
120- if ( comboBox . SelectedItem is null )
125+ if ( ! isOpenDown )
126+ {
127+ //get container at the bottom of viewport
128+ //ComboBox ItemContainerGenerator block size is 15
129+ container = comboBox . ItemContainerGenerator . ContainerFromIndex ( Math . Min ( 15 , comboBox . Items . Count ) - 1 ) ;
130+ }
131+ else if ( comboBox . SelectedItem is null )
121132 {
122133 container = comboBox . ItemContainerGenerator . ContainerFromIndex (
123134 ( int ) Math . Ceiling ( comboBox . Items . Count / 2.0 ) ) ;
124-
125- if ( comboBox . ItemContainerGenerator . ContainerFromIndex ( 0 ) is ComboBoxItem item )
126- {
127- var highlightedInfoProperty = typeof ( ComboBox ) . GetProperty ( "HighlightedInfo" ,
128- BindingFlags . Instance | BindingFlags . NonPublic ) ;
129-
130- var setter = highlightedInfoProperty . SetMethod ;
131-
132- var itemInfo = typeof ( ComboBox )
133- . GetMethod ( "ItemInfoFromContainer" , BindingFlags . Instance | BindingFlags . NonPublic ) ?
134- . Invoke ( comboBox , [ item ] ) ;
135-
136- setter ? . Invoke ( comboBox , [ itemInfo ] ) ;
137- }
135+ TryHighlightingFirstItem ( comboBox ) ;
138136 }
139137 else
140138 {
@@ -144,6 +142,25 @@ private static FrameworkElement GetToAlignContainer(ComboBox comboBox)
144142 return container as FrameworkElement ;
145143 }
146144
145+ private static void TryHighlightingFirstItem ( ComboBox comboBox )
146+ {
147+ if ( comboBox . ItemContainerGenerator . ContainerFromIndex ( 0 ) is not ComboBoxItem item )
148+ {
149+ return ;
150+ }
151+
152+ var highlightedInfoProperty = typeof ( ComboBox ) . GetProperty ( "HighlightedInfo" ,
153+ BindingFlags . Instance | BindingFlags . NonPublic ) ;
154+
155+ var setter = highlightedInfoProperty ? . SetMethod ;
156+
157+ var itemInfo = typeof ( ComboBox )
158+ . GetMethod ( "ItemInfoFromContainer" , BindingFlags . Instance | BindingFlags . NonPublic ) ?
159+ . Invoke ( comboBox , [ item ] ) ;
160+
161+ setter ? . Invoke ( comboBox , [ itemInfo ] ) ;
162+ }
163+
147164 private static void UpdateCornerRadius ( ComboBox comboBox , Popup ? popup , bool isDropDownOpen , bool isOpenDown )
148165 {
149166 var textBoxRadius = ControlHelper . GetCornerRadius ( comboBox ) ;
@@ -194,17 +211,15 @@ private static CornerRadius GetFilteredPopupRadius(CornerRadius popupRadius, boo
194211
195212 private static bool IsPopupOpenDown ( ComboBox comboBox , double popupVerticalOffset )
196213 {
197- double verticalOffset = popupVerticalOffset ;
198- if ( GetTemplateChild < Border > ( c_popupBorderName , comboBox ) is { } popupBorder )
214+ if ( GetTemplateChild < Border > ( c_popupBorderName , comboBox ) is not { } popupBorder ||
215+ GetTemplateChild < TextBox > ( c_editableTextName , comboBox ) is not { } textBox )
199216 {
200- if ( GetTemplateChild < TextBox > ( c_editableTextName , comboBox ) is { } textBox )
201- {
202- var popupTop = popupBorder . TranslatePoint ( new Point ( 0 , 0 ) , textBox ) ;
203- verticalOffset = popupTop . Y ;
204- }
217+ return false ;
205218 }
206-
207- return verticalOffset > popupVerticalOffset ;
219+
220+ var popupTopPoint = popupBorder . TranslatePoint ( new Point ( 0 , 0 ) , textBox ) ;
221+
222+ return popupTopPoint . Y + popupVerticalOffset > 0 ;
208223 }
209224
210225 private static object ResourceLookup ( Control control , object key )
0 commit comments