WPF behavior InvokeCommandAction CommadParameter pass selectedItem of currentcontrol

 <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}"
                                           CommandParameter="{Binding Path=SelectedItem,
                 RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}}"/>
         </behavior:EventTrigger>
     </behavior:Interaction.Triggers>
</ListBox>



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

private void SelectionChangedCmdExecuted(object obj)
{
    var bk = obj as Book;
    if (bk != null)
    {
        ImgTitle = bk.Name;
        MessageBox.Show(ImgTitle, "Image Title", MessageBoxButton.OK);
    }
}

 

 

 

 

 

 

 

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