■ Behavior<T> 클래스를 사용해 ListBox 객체에서 항목 선택시 해당 항목으로 자동 스크롤하는 방법을 보여준다.
▶ AutoScrollListBoxBehavior.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
using System; using System.Windows.Controls; using System.Windows.Threading; using Microsoft.Xaml.Behaviors; namespace TestProject { /// <summary> /// 자동 스크롤 리스트 박스 동작 /// </summary> public class AutoScrollListBoxBehavior : Behavior<ListBox> { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 부착시 처리하기 - OnAttached() /// <summary> /// 부착시 처리하기 /// </summary> protected override void OnAttached() { base.OnAttached(); AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged; } #endregion #region 탈착시 처리하기 - OnDetaching() /// <summary> /// 탈착시 처리하기 /// </summary> protected override void OnDetaching() { AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged; base.OnDetaching(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 관련 객체 선택 변경시 처리하기 - AssociatedObject_SelectionChanged(sender, e) /// <summary> /// 관련 객체 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox listBox = sender as ListBox; if(listBox?.SelectedItem == null) { return; } Action action = () => { listBox.UpdateLayout(); if(listBox.SelectedItem != null) { listBox.ScrollIntoView(listBox.SelectedItem); } }; listBox.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel VerticalAlignment="Center"> <ListBox Name="listBox" Width="200" Height="200"> <ListBoxItem>항목 01</ListBoxItem> <ListBoxItem>항목 02</ListBoxItem> <ListBoxItem>항목 03</ListBoxItem> <ListBoxItem>항목 04</ListBoxItem> <ListBoxItem>항목 05</ListBoxItem> <ListBoxItem>항목 06</ListBoxItem> <ListBoxItem>항목 07</ListBoxItem> <ListBoxItem>항목 08</ListBoxItem> <ListBoxItem>항목 09</ListBoxItem> <ListBoxItem>항목 10</ListBoxItem> <ListBoxItem>항목 11</ListBoxItem> <ListBoxItem>항목 12</ListBoxItem> <ListBoxItem>항목 13</ListBoxItem> <ListBoxItem>항목 14</ListBoxItem> <ListBoxItem>항목 15</ListBoxItem> <ListBoxItem>항목 16</ListBoxItem> <ListBoxItem>항목 17</ListBoxItem> <ListBoxItem>항목 18</ListBoxItem> <ListBoxItem>항목 19</ListBoxItem> <ListBoxItem>항목 20</ListBoxItem> <i:Interaction.Behaviors> <local:AutoScrollListBoxBehavior /> </i:Interaction.Behaviors> </ListBox> <StackPanel Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Name="firstItemButton" Width="100" Height="30" Content="첫번째 항목" /> <Button Name="lastItemButton" Margin="10 0 0 0" Width="100" Height="30" Content="마지막 항목" /> </StackPanel> </StackPanel> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System.Windows; namespace TestProject; /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.firstItemButton.Click += firstItemButton_Click; this.lastItemButton.Click += lastItemButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 첫번째 항목 버튼 클릭시 처리하기 - firstItemButton_Click(sender, e) /// <summary> /// 첫번째 항목 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void firstItemButton_Click(object sender, RoutedEventArgs e) { this.listBox.SelectedIndex = 0; } #endregion #region 마지막 항목 버튼 클릭시 처리하기 - lastItemButton_Click(sender, e) /// <summary> /// 마지막 항목 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void lastItemButton_Click(object sender, RoutedEventArgs e) { this.listBox.SelectedIndex = this.listBox.Items.Count - 1; } #endregion } |