在ComboBox(WPF)中禁用滚动鼠标悬停

时间:2017-08-08 11:33:34

标签: c# wpf visual-studio-2017

是否可以在ComboBox下拉列表中禁用自动滚动效果?问题是当鼠标悬停在打开的ComboBox的最后一个元素上时,它会自动滚动到下一个元素。

在我的情况下,ComboBox位于屏幕的底部,列表向上显示,当我打开ComboBox并移动鼠标时,它立即向下滚动..我的XAML看起来像这样:

<ComboBox x:Name="serverSelection" ScrollViewer.CanContentScroll="False" Width="268" Height="54" FontSize="18" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" SelectedIndex="0">
    <ComboBoxItem Content="xenappts01" HorizontalAlignment="Left" Width="280" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
    <ComboBoxItem Content="xenappts02" HorizontalAlignment="Left" Width="280" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
    <ComboBoxItem Content="xenappts03" HorizontalAlignment="Left" Width="280" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
    <ComboBoxItem Content="xenappts04" HorizontalAlignment="Left" Width="280" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
    <ComboBoxItem Content="xenappts05" HorizontalAlignment="Left" Width="280" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
    <ComboBoxItem Content="xenappts05c" HorizontalAlignment="Left" Width="280" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
</ComboBox>

我尝试设置ScrollViewer.CanContentScroll="False",但这不起作用。

这就是它的样子:

https://i.stack.imgur.com/GcwJX.gif

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您可以处理RequestBringIntoView容器的ComboBoxItem事件:

<ComboBox>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <EventSetter Event="RequestBringIntoView" Handler="ComboBox_RequestBringIntoView"/>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
private void ComboBox_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
    e.Handled = true;
}