■ Behavior<T> 클래스를 사용해 배타적 익스팬더 동작을 만드는 방법을 보여준다.
▶ ExclusiveExpanderBehavior.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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
using Microsoft.Xaml.Behaviors; using System.Windows; using System.Windows.Controls; namespace TestProject { /// <summary> /// 배타적 익스팬더 동작 /// </summary> public class ExclusiveExpanderBehavior : Behavior<Panel> { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 부착시 처리하기 - OnAttached() /// <summary> /// 부착시 처리하기 /// </summary> protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += AssociatedObject_Loaded; } #endregion #region 탈착시 처리하기 - OnDetaching() /// <summary> /// 탈착시 처리하기 /// </summary> protected override void OnDetaching() { AssociatedObject.Loaded -= AssociatedObject_Loaded; base.OnDetaching(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 결합 객체 로드시 처리하기 - AssociatedObject_Loaded(sender, e) /// <summary> /// 결합 객체 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { foreach(UIElement child in AssociatedObject.Children) { if(child == null) { return; } Expander expander = child as Expander; if(expander != null) { expander.Expanded += expander_Expanded; } } } #endregion #region 익스팬더 확장시 처리하기 - expander_Expanded(sender, e) /// <summary> /// 익스팬더 확장시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void expander_Expanded(object sender, RoutedEventArgs e) { foreach(UIElement child in AssociatedObject.Children) { if(child == null) { return; } Expander childExpander = child as Expander; if(childExpander != null) { childExpander.IsExpanded = false; } } Expander expander = sender as Expander; if(expander == null) { return; } expander.Expanded -= expander_Expanded; expander.IsExpanded = true; expander.Expanded += expander_Expanded; } #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 57 |
<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="Behavior<T> 클래스 : 배타적 익스팬더 동작 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel VerticalAlignment="Center" Width="300" Background="Turquoise"> <i:Interaction.Behaviors> <local:ExclusiveExpanderBehavior /> </i:Interaction.Behaviors> <TextBlock Margin="10" FontWeight="Bold" Text="1개의 Expander만 확장할 수 있다." /> <Expander Header="테스트 1" IsExpanded="True"> <TextBlock Margin="10" TextWrapping="Wrap"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </TextBlock> </Expander> <Expander Header="테스트 2"> <TextBlock Margin="10" TextWrapping="Wrap"> It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </TextBlock> </Expander> <Expander Header="테스트 3"> <TextBlock Margin="10" TextWrapping="Wrap"> It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </TextBlock> </Expander> </StackPanel> </Grid> </Window> |