WPF behavior InvokeCommndAction PassEventArgsToCommand

//xaml
  <ListBox x:Name="lbx"
           SelectedIndex="0"
           ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
           VirtualizingPanel.IsContainerVirtualizable="True"
           VirtualizingPanel.IsVirtualizing="True"
           VirtualizingPanel.ScrollUnit="Item"
           VirtualizingPanel.VirtualizationMode="Recycling">
      <behavior:Interaction.Triggers>
          <behavior:EventTrigger EventName="SelectionChanged">               
              <behavior:InvokeCommandAction Command="{Binding SelectionChangedCmd}"
                                            PassEventArgsToCommand="True"/>
          </behavior:EventTrigger>
      </behavior:Interaction.Triggers>
</ListBox>


//cs
 private void InitCmds()
 {
     SelectionChangedCmd = new DelCmd(SelectionChangedCmdExecuted);
 }

 private void SelectionChangedCmdExecuted(object obj)
 {
     if(obj!=null)
     {
         var eventArgs = obj as SelectionChangedEventArgs;
         if(eventArgs!=null && eventArgs.AddedItems!=null && eventArgs.AddedItems.Count>0)
         {
             var bk = eventArgs.AddedItems[0] as Book;
             if(bk!=null)
             {
                 ImgTitle = bk.Name;
             }
         }
     }
 }

 

 

 

 

 

posted @ 2024-09-20 11:31  FredGrit  阅读(6)  评论(0编辑  收藏  举报