XAML WPF -将事件绑定到ViewModel

nom7f22z  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(284)

我有一个堆栈面板,我需要在ViewModel中处理一个鼠标事件,但是绑定不起作用--找不到命令,尽管其他元素也绑定到ViewModel并且它们都起作用了。

<Window.DataContext>
        <vm:Ticker/>
</Window.DataContext>

 <ListView Grid.Row="3"
     ItemsSource ="{Binding TickersCollectionView}">
   <ListView.ItemTemplate>
     <DataTemplate>
        <StackPanel Orientation="Horizontal" 
                    Name="STPListView">

                  <i:Interaction.Triggers>
                     <i:EventTrigger EventName="MouseLeftButtonDown">
                       <i:InvokeCommandAction
                          Command="{Binding MouseLeftButtonDownCommand}"
                          CommandParameter="{Binding ElementName=STPListView}"/>
                     </i:EventTrigger>
                  </i:Interaction.Triggers>
          <TextBlock 
              Width="130" 
              Text="{Binding Market}"
              Foreground="WhiteSmoke"/>
         <TextBlock 
              Width="110" 
              Text="{Binding Price}"
              Foreground="{Binding LastPrice, Converter={StaticResource BoolToForeground}}"/>
      </StackPanel>
    </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

该命令是ViewModel的属性,如何绑定?

ljsrvy3e

ljsrvy3e1#

如果MouseLeftButtonDownCommand属性与TickersCollectionView属性定义在同一个视图模型中,则应使用RelativeSource

Command="{Binding DataContext.MouseLeftButtonDownCommand,
    RelativeSource={RelativeSource AncestorType=ListView}}"

相关问题