Can I enable PreviewClick using InputBindings in WPF?

我只是一个虾纸丫 提交于 2019-12-02 19:59:11

问题


I want to detect when a user clicks on an item on a listview, without using events as I do command binding and I don't like all the nonsense of the behaviours. I have tried this:

<ListView x:Name="MainList" Margin="2,8,6,8" Background="Black" 
   ItemsSource="{Binding Path=AssetsVM.Data, Mode=OneWay}" 
   BorderBrush="{x:Null}" >

    <ListView.InputBindings>
         <MouseBinding Command="{Binding Path=AssetsVM.SelectActivo}" 
            CommandParameter="{Binding ElementName=MainList, Path=SelectedItem}" 
            MouseAction="LeftClick" />
    </ListView.InputBindings>

This works fine if I click on the listview but does not work on the items. What I need is either a way to enable "Preview" or have a MouseAction/Gesture that behaves as preview. Are either one of these possible?


回答1:


When using a command-driven architecture like this, I usually use AttachedCommandBehavior to get around the fact that Microsoft did not make MouseBinding.Command a DependencyProperty. An example of how to do get the functionality you want using this approach is shown below:

<ListView x:Name="MainList" ItemsSource="{Binding Path=AssetsVM.Data, Mode=OneWay}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Setters>
                <Setter Property="acb:CommandBehavior.Event" Value="Selected" />
                <Setter Property="acb:CommandBehavior.Command" Value="{Binding DataContext.AssetsVM.SelectActivo, ElementName=MainList}" />
                <Setter Property="acb:CommandBehavior.CommandParameter" Value="{Binding}" />
            </Style.Setters>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>


来源:https://stackoverflow.com/questions/2590924/can-i-enable-previewclick-using-inputbindings-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!