TriggerParameterPath的用法

1、先介绍TriggerParameterPath的AddedItems方法吧
设计场景
点击用户名罗列出用户的详细信息,或者点击配方罗列出配方的详细信息
在WPF的官方例子中,它是这样写的
有上下两层,点击第一行的用户名,第二行罗列对应的信息

 <!--  可以用来罗列信息,不同的值对应不同的信息  -->
 <StackPanel>
     <TextBlock
         Margin="5,15,0,10"
         FontFamily="Verdana"
         FontSize="11"
         FontWeight="Bold">
         My Friends:
     </TextBlock>

     <!--  如果ListBox不写模板的话,只绑定第一项list[0]  -->
     <ListBox
         Width="200"
         IsSynchronizedWithCurrentItem="True"
         ItemsSource="{Binding Source={StaticResource MyFriends}}" />
     <TextBlock
         Margin="5,15,0,5"
         FontFamily="Verdana"
         FontSize="11"
         FontWeight="Bold">
         Information:
     </TextBlock>

     <!--  罗列信息,写一个内容模板  -->
     <ContentControl Content="{Binding Source={StaticResource MyFriends}}" ContentTemplate="{StaticResource DetailTemplate}" />
 </StackPanel>

它使用了ContentControl,也就是说上下两层其实绑定的都是同一组数据,然后写了 IsSynchronizedWithCurrentItem="True" 让上下两层数据保持同步,那么点击用户名罗列用户信息就实现了
接下来我们说说Prism的TriggerParameterPath
在Prism的官方例子中

 <ListBox
     Grid.Row="1"
     Margin="5"
     ItemsSource="{Binding Items}"
     SelectionMode="Single">
     <i:Interaction.Triggers>
         <!--  This event trigger will execute the action when the corresponding event is raised by the ListBox.  -->
         <i:EventTrigger EventName="SelectionChanged">
             <!--  This action will invoke the selected command in the view model and pass the parameters of the event to it.  -->
             <prism:InvokeCommandAction Command="{Binding SelectedCommand}" TriggerParameterPath="AddedItems" />
         </i:EventTrigger>
     </i:Interaction.Triggers>
 </ListBox>
 <StackPanel
    Grid.Row="2"
    Margin="5"
    Orientation="Horizontal">
    <TextBlock FontWeight="Bold" Foreground="DarkRed">Selected Item:</TextBlock>
    <TextBlock
        Margin="5,0"
        AutomationProperties.AutomationId="SelectedItemTextBlock"
        FontWeight="Bold"
        Foreground="DarkRed"
        Text="{Binding SelectedItemText}" />
</StackPanel>

后台代码

 public class MainWindowViewModel : BindableBase
 {
     private string _title = "Prism Unity Application";
     public string Title
     {
         get { return _title; }
         set { SetProperty(ref _title, value); }
     }

     private string _selectedItemText;
     public string SelectedItemText
     {
         get { return _selectedItemText; }
         private set { SetProperty(ref _selectedItemText, value); }
     }

     public IList<string> Items { get; private set; }

     public DelegateCommand<object[]> SelectedCommand { get; private set; }

     public MainWindowViewModel()
     {
         Items = new List<string>();

         Items.Add("Item1");
         Items.Add("Item2");
         Items.Add("Item3");
         Items.Add("Item4");
         Items.Add("Item5");

         // This command will be executed when the selection of the ListBox in the view changes.
         SelectedCommand = new DelegateCommand<object[]>(OnItemSelected);
     }

     private void OnItemSelected(object[] selectedItems)
     {
         if (selectedItems != null && selectedItems.Count() > 0)
         {
             SelectedItemText = selectedItems.FirstOrDefault().ToString();   //返回序列中第一个元素
         }
     }
 }

EventTrigger:
<i:EventTrigger EventName="SelectionChanged"> 这行代码创建了一个事件触发器,它监听 ListBox 控件上的 SelectionChanged 事件。

InvokeCommandAction:
<prism:InvokeCommandAction Command="{Binding SelectedCommand}" TriggerParameterPath="AddedItems" /> 这行代码定义了一个动作,当事件触发器检测到事件时执行。Command 属性绑定到 ViewModel 中的 SelectedCommand 属性,这意味着它将执行与该属性关联的命令。

TriggerParameterPath:
TriggerParameterPath="AddedItems" 指定了传递给命令的参数来源。在这里,AddedItems 将你点击的SelectedItem传递给后台命令的参数

posted @ 2024-07-09 22:33  孤沉  阅读(2)  评论(0编辑  收藏  举报