ComboBox不用取得邏輯焦點(!IsFocused),只要滑鼠在ComboBox上方(IsMouseOver),移動滾輪(MouseWheelEventArgs.Delta > 0 or < 0),就能調整選項
延續之前的文章,ItemsControl.ItemsSource關聯字串(string)型態資料檢視,繼續以ComboBoxOrderBuySell作為範例介紹
MouseWheelEventArgs.Delta > 0,代表滑鼠滾輪向上滾(圖左,遠離使用者),< 0,代表向下滾(圖右,靠近使用者)
XAML:
<Window <-- -->
Title="MainWindow" Height="990" Width="1760" PreviewMouseWheel="Window_PreviewMouseWheel">
<Grid>
<Label Content="Buy/Sell" HorizontalAlignment="Left" Margin="260,10,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="ComboBoxOrderBuySell" ToolTip="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}" HorizontalAlignment="Left" Margin="260,35,0,0" VerticalAlignment="Top" Width="120" IsEditable="True"/>
C#:
void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Delta == 0)
{
return;
}
ComboBox cb = null;
if (ComboBoxOrderBuySell.IsMouseOver && !ComboBoxOrderBuySell.IsFocused)
{
cb = ComboBoxOrderBuySell;
}
//else if //其他的ComboBox
if (cb == null)
{
return;
}
int nextIdx = cb.SelectedIndex + (e.Delta > 0 ? -1 : 1);
if (nextIdx >= 0 && nextIdx < cb.Items.Count)
{
cb.SelectedIndex = nextIdx;
}
}