11using System ;
2+ using System . Diagnostics ;
23using System . Reflection ;
34using System . Windows ;
45using System . Windows . Controls ;
56using System . Windows . Controls . Primitives ;
67using System . Windows . Input ;
8+ using System . Windows . Media ;
79using iNKORE . UI . WPF . Converters ;
810using iNKORE . UI . WPF . Modern . Controls . Primitives ;
911
@@ -28,8 +30,9 @@ static WinUIComboBoxBehaviorHelper()
2830 /// 1. align selected container in popup to combobox.
2931 /// 2. in case of no selection, first in popup is highlighted (done)
3032 /// 3. mouse hovering shouldn't trigger focus ?! (done)
31- /// 4. persist selected item only when drop down closes?? (not done)
32- /// 5. KeepInteriorCornersSquare (already done)
33+ /// 4. KeepInteriorCornersSquare (already done)
34+ /// 5. light dismiss behavior only if click inside the window (not done)
35+ /// 6. popup bounded by the screen (not done)
3336 /// </summary>
3437 public static readonly DependencyProperty IsEnabledProperty =
3538 DependencyProperty . RegisterAttached (
@@ -99,30 +102,31 @@ private static void AlignSelectedContainer(ComboBox comboBox, Popup popup)
99102 return ;
100103 }
101104
102- var mockContainer = ( comboBox . ItemContainerGenerator . ContainerFromIndex ( 0 ) as ComboBoxItem ) ! ;
105+ var firstContainer = ( comboBox . ItemContainerGenerator . ContainerFromIndex ( 0 ) as ComboBoxItem ) ! ;
103106 var scrollViewer = GetTemplateChild < ScrollViewer > ( "ScrollViewer" , comboBox ) ;
104107
105- var toSelectIndex = comboBox . SelectedIndex ;
106- if ( toSelectIndex < 0 )
108+ var toCenterIndex = comboBox . SelectedIndex ;
109+ if ( toCenterIndex < 0 )
107110 {
108- toSelectIndex = ( int ) Math . Ceiling ( comboBox . Items . Count / 2.0 ) ;
111+ toCenterIndex = ( int ) Math . Ceiling ( comboBox . Items . Count / 2.0 ) ;
109112 TryHighlightingFirstItem ( comboBox ) ;
110113 }
111- else if ( mockContainer . ActualHeight * comboBox . Items . Count > comboBox . MaxDropDownHeight )
114+ else if ( firstContainer . ActualHeight * comboBox . Items . Count > comboBox . MaxDropDownHeight )
112115 {
113- toSelectIndex = 0 ;
116+ var maxVisible = Math . Floor ( comboBox . MaxDropDownHeight / firstContainer . ActualHeight ) ;
117+ toCenterIndex = ( int ) Math . Floor ( maxVisible / 2.0 ) ;
114118 }
115119
116120 var paddingBorder = ( scrollViewer . Parent as Border ) ! ;
117121
118122 if ( IsPopupOpenDown ( comboBox , popup . VerticalOffset ) )
119123 {
120- popup . VerticalOffset = - mockContainer . ActualHeight * ( toSelectIndex + 1 ) - paddingBorder . Padding . Top ;
124+ popup . VerticalOffset = - firstContainer . ActualHeight * ( toCenterIndex + 1 ) - paddingBorder . Padding . Top ;
121125 }
122126 else
123127 {
124128 popup . VerticalOffset =
125- mockContainer . ActualHeight * ( Math . Min ( ( int ) ( comboBox . MaxDropDownHeight / mockContainer . ActualHeight ) , comboBox . Items . Count ) - toSelectIndex ) +
129+ firstContainer . ActualHeight * ( Math . Min ( ( int ) ( comboBox . MaxDropDownHeight / firstContainer . ActualHeight ) , comboBox . Items . Count ) - toCenterIndex ) +
126130 paddingBorder . Padding . Bottom ;
127131 }
128132 }
0 commit comments