프로그램 개발/C#

[wpf] ScrollViewer와 Mouse Wheel Event

(ㅇㅅㅎ) 2020. 12. 7. 15:18
728x90
반응형

1. 위아래 Mouse Wheel Event

특별한 설정이 필요 없이 기본적으로 ScrollViewer를 사용하면 됩니다. 

 

xaml 코드
    <ScrollViewer VerticalScrollBarVisibility="Hidden">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <Button Content="1"/>
                <Button Content="2"/>
                <Button Content="3"/>
                <Button Content="4"/>
                <Button Content="5"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Button Content="6"/>
                <Button Content="7"/>
                <Button Content="8"/>
                <Button Content="9"/>
                <Button Content="10"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Button Content="11"/>
                <Button Content="12"/>
                <Button Content="13"/>
                <Button Content="14"/>
                <Button Content="15"/>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>

 

2. 좌우 Mouse Wheel Event

좌우 Mouse Wheel Event를 하기 위해서는 HorizontalScrollBarVisibility와 PreviewMouseWheel 이벤트를 설정해 주어야 합니다.

xaml 코드
    <ScrollViewer
        VerticalScrollBarVisibility="Hidden"
        HorizontalScrollBarVisibility="Hidden"
        PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <Button Content="1"/>
                <Button Content="2"/>
                <Button Content="3"/>
                <Button Content="4"/>
                <Button Content="5"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Button Content="6"/>
                <Button Content="7"/>
                <Button Content="8"/>
                <Button Content="9"/>
                <Button Content="10"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Button Content="11"/>
                <Button Content="12"/>
                <Button Content="13"/>
                <Button Content="14"/>
                <Button Content="15"/>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
cs 코드
    private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        ScrollViewer sv = sender as ScrollViewer;
        if (0 > e.Delta) {
                sv.LineRight();
        }else{
                sv.LineLeft();
        }
        e.Handled = true;
    }
반응형