■ StandardUICommand 클래스의 ExecuteRequested 이벤트를 사용해 명령을 실행하는 방법을 보여준다.
▶ ListItemData.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 |
using System.Windows.Input; namespace TestProject { /// <summary> /// 리스트 항목 데이터 /// </summary> public class ListItemData { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { get; set; } #endregion #region 명령 - Command /// <summary> /// 명령 /// </summary> public ICommand Command { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return Text; } #endregion } } |
▶ MainPage.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 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 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Page.Resources> <Style x:Key="HorizontalSwipe" TargetType="ListViewItem" BasedOn="{StaticResource DefaultListViewItemStyle}"> <Setter Property="Height" Value="60" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Padding" Value="0" /> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Stretch" /> </Style> </Page.Resources> <Grid Name="rootGrid" Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <MenuBar Grid.Row="0"> <MenuBarItem Title="File"> <MenuFlyoutItem Text="New" /> <MenuFlyoutItem Text="Open..." /> <MenuFlyoutItem Text="Save" /> <MenuFlyoutItem Text="Exit" /> </MenuBarItem> <MenuBarItem Title="Edit"> <MenuFlyoutItem Name="deleteFlyoutItem" /> </MenuBarItem> <MenuBarItem Title="Help"> <MenuFlyoutItem Text="About" /> </MenuBarItem> </MenuBar> <ListView Name="listView" Grid.Row="1" ItemContainerStyle="{StaticResource HorizontalSwipe}" Height="500" IsItemClickEnabled="True" SelectionMode="Single"> <ListView.ItemTemplate> <DataTemplate x:DataType="local:ListItemData"> <UserControl PointerEntered="listViewItemControl_PointerEntered" PointerExited="listViewItemControl_PointerExited"> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="HoveringStates"> <VisualState x:Name="HoverButtonsHidden" /> <VisualState x:Name="HoverButtonsShown"> <VisualState.Setters> <Setter Target="hoverButton.Visibility" Value="Visible" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <SwipeControl> <SwipeControl.RightItems> <SwipeItems Mode="Execute"> <SwipeItem Command="{x:Bind Command}" CommandParameter="{x:Bind Text}" /> </SwipeItems> </SwipeControl.RightItems> <Grid VerticalAlignment="Center"> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10" FontSize="18" Text="{x:Bind Text}" /> <AppBarButton x:Name="hoverButton" HorizontalAlignment="Right" IsTabStop="False" Command="{x:Bind Command}" CommandParameter="{x:Bind Text}" Visibility="Collapsed" /> </Grid> </SwipeControl> </Grid> </UserControl> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </Page> |
▶ MainPage.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 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
using System.Collections.ObjectModel; using Windows.Foundation.Metadata; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Input; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 컬렉션 /// </summary> private ObservableCollection<ListItemData> collection = new ObservableCollection<ListItemData>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); Loaded += Page_Loaded; this.listView.Loaded += listView_Loaded; this.listView.ContainerContentChanging += listView_ContainerContentChanging; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 페이지 로드시 처리하기 - Page_Loaded(sender, e) /// <summary> /// 페이지 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Page_Loaded(object sender, RoutedEventArgs e) { if(ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 7)) { StandardUICommand deleteCommand = new StandardUICommand(StandardUICommandKind.Delete); deleteCommand.ExecuteRequested += deleteCommand_ExecuteRequested; this.deleteFlyoutItem.Command = deleteCommand; for(int i = 0; i < 15; i++) { this.collection.Add ( new ListItemData { Text = "List item " + i.ToString(), Command = deleteCommand } ); } } else { for(int i = 0; i < 15; i++) { this.collection.Add ( new ListItemData { Text = "List item " + i.ToString(), Command = null } ); } } } #endregion #region 리스트 뷰 로드시 처리하기 - listView_Loaded(sender, e) /// <summary> /// 리스트 뷰 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listView_Loaded(object sender, RoutedEventArgs e) { ListView listView = sender as ListView; listView.ItemsSource = this.collection; } #endregion #region 리스트 뷰 컨테이너 컨텐트 변경시 처리하기 - listView_ContainerContentChanging(sender, e) /// <summary> /// 리스트 뷰 컨테이너 컨텐트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs e) { ListItemData listItemData = e.Item as ListItemData; MenuFlyoutItem menuFlyoutItem = new MenuFlyoutItem() { Command = listItemData.Command }; MenuFlyout menuFlyout = new MenuFlyout(); menuFlyout.Items.Add(menuFlyoutItem); menuFlyout.Opened += menuFlyout_Opened; e.ItemContainer.ContextFlyout = menuFlyout; } #endregion #region 삭제 명령 실행 요청시 처리하기 - deleteCommand_ExecuteRequested(sender, e) /// <summary> /// 삭제 명령 실행 요청시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void deleteCommand_ExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs e) { if(e.Parameter != null) { foreach(ListItemData listItemData in this.collection) { if(listItemData.Text == (e.Parameter as string)) { this.collection.Remove(listItemData); return; } } } if(this.listView.SelectedIndex != -1) { this.collection.RemoveAt(this.listView.SelectedIndex); } } #endregion #region 메뉴 플라이아웃 오픈시 처리하기 - menuFlyout_Opened(sender, e) /// <summary> /// 메뉴 플라이아웃 오픈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void menuFlyout_Opened(object sender, object e) { MenuFlyout menuFlyout = sender as MenuFlyout; ListViewItem listViewItem = menuFlyout.Target as ListViewItem; listViewItem.IsSelected = true; } #endregion #region 리스트 뷰 항목 컨트롤 포인터 진입시 처리하기 - listViewItemControl_PointerEntered(sender, e) /// <summary> /// 리스트 뷰 항목 컨트롤 포인트 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listViewItemControl_PointerEntered(object sender, PointerRoutedEventArgs e) { if(e.Pointer.PointerDeviceType == PointerDeviceType.Mouse || e.Pointer.PointerDeviceType == PointerDeviceType.Pen) { VisualStateManager.GoToState(sender as Control, "HoverButtonsShown", true); } } #endregion #region 리스트 뷰 항목 컨트롤 포인터 이탈시 처리하기 - listViewItemControl_PointerExited(sender, e) /// <summary> /// 리스트 뷰 항목 컨트롤 포인터 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listViewItemControl_PointerExited(object sender, PointerRoutedEventArgs e) { VisualStateManager.GoToState(sender as Control, "HoverButtonsHidden", true); } #endregion } } |