티스토리 뷰

Previous Platforms

InvokeActionCommandEx

kaki104 2013. 11. 21. 11:51
반응형

Behavior SDK를 이용해서 작업을 하다보니 ItemClick 이벤트에 대한 처리를 할 수 없다는 걸 알았다.

그래서 InvokeActionCommandEx라는 IAction 클래스를 만들어 보았다.

 

InvokeActionCommand를 CommandParameter를 사용하지 않으면 기본적으로 이벤트 아규먼트를 반환한다.

즉, 아래의 내용은 참고로만 사용하도록 한다.

 

    public class InvokeActionCommandEx : DependencyObject, Microsoft.Xaml.Interactivity.IAction
    {
        #region Static Fields

 

        /// <summary>
        /// Defines the CommandParameter dependency property, of type <see cref="object"/>.
        /// </summary>
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
            "CommandParameter",
            typeof(object),
            typeof(InvokeActionCommandEx),
            new PropertyMetadata(null));

 

        /// <summary>
        /// Defines the Command dependency property, of type <see cref="ICommand"/>.
        /// </summary>
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command",
            typeof(ICommand),
            typeof(InvokeActionCommandEx),
            new PropertyMetadata(null));

 

        /// <summary>
        /// Defines the Command dependency property, of type <see cref="PassEventArgsToCommand"/>.
        /// </summary>
        public static readonly DependencyProperty PassEventArgsToCommandProperty = DependencyProperty.Register(
            "PassEventArgsToCommand",
            typeof(bool),
            typeof(InvokeActionCommandEx),
            new PropertyMetadata(false));

 

        #endregion

 

        #region Public Properties

        /// <summary>
        /// Gets or sets the command to be invoked.
        /// </summary>
        public ICommand Command
        {
            get
            {
                return (ICommand)this.GetValue(CommandProperty);
            }

            set
            {
                this.SetValue(CommandProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the command parameter to pass to the <see cref="ICommand"/> upon invocation.
        /// </summary>
        /// <remarks>
        /// This takes precedence over the <see cref="PassEventArgsToCommand"/> property - if <see cref="CommandParameter"/>
        /// is specified, then <see cref="PassEventArgsToCommand"/> is ignored.
        /// </remarks>
        public object CommandParameter
        {
            get
            {
                return this.GetValue(CommandParameterProperty);
            }

            set
            {
                this.SetValue(CommandParameterProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not the event arguments associated to the raised
        /// event should be passed to the command.
        /// </summary>
        public bool PassEventArgsToCommand
        {
            get
            {
                return (bool)this.GetValue(PassEventArgsToCommandProperty);
            }

            set
            {
                this.SetValue(PassEventArgsToCommandProperty, value);
            }
        }
        #endregion

 

        public object Execute(object sender, object parameter)
        {
            FrameworkElement element = sender as FrameworkElement;
    
            if (element != null && Command != null && Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter == null && PassEventArgsToCommand == true ? parameter : this.CommandParameter);
            }
    
          return null;
        }

    }

 

 

XAML에서 사용

 

    xmlns:t="using:Microsoft.Xaml.Interactivity"
    xmlns:i="using:KTour.Behaviors"        //본인이 만든위치의 네임스페이스를 사용하면 된다.

 

                    <ListView Margin="0,0,0,10" Grid.Column="1" ItemsSource="{Binding SiGunGu}"
                              ItemTemplate="{StaticResource SiGunGuItemTemplate}"
                  IsItemClickEnabled="True" SelectedItem="{Binding SelectedSiGunGu, Mode=TwoWay}" Style="{StaticResource RemoveScrollViewerListViewStyle}"
                              BorderBrush="{StaticResource PageBackgroundBrush}" BorderThickness="1" ItemContainerStyle="{StaticResource ListViewItemContainerStyle}">
                        <t:Interaction.Behaviors>
                            <c:EventTriggerBehavior EventName="ItemClick">
                                <i:InvokeActionCommandEx Command="{Binding SiGunGuClickCommand, Mode=OneWay}" PassEventArgsToCommand="True"/>
                            </c:EventTriggerBehavior>
                        </t:Interaction.Behaviors>
                    </ListView>

 

ListView, GridView에서 ItemClick이벤트를 사용하는 경우 위와 같이 사용한다.

 

반응형

'Previous Platforms' 카테고리의 다른 글

Nokia Lumia 925 Get!  (0) 2013.12.03
Windows Store languages country list  (0) 2013.11.26
Behaviors SDK  (0) 2013.11.20
Windows 8 App Award  (0) 2013.11.19
Korea Tour 개발 후기  (0) 2013.10.11
댓글