使用附加命令为listview触发双击事件 - MVVM

时间:2014-01-09 09:08:43

标签: wpf listview mvvm attached-properties

我正在使用附加命令为Listview中的项目触发双击事件,我已经使用了marlon grechs博客中写的方法 - (AttachedCommandBehavior V2又称ACB),因为我正在使用MVVM!

我已经在一个小的测试示例中使用它并且它工作正常,问题是,当我将它集成到应用程序中时,每次打开页面时都会出现异常/错误,其中使用了附加属性。

例外说:

Invalid Operation Exception - Could not resolve event name 
{Void BindEvent(System.Windows.DependencyObject, System.String)}

并指向Eventhandler定义

    EventHandler = EventHandlerGenerator.CreateDelegate(
        Event.EventHandlerType, typeof(CommandBehaviorBinding).GetMethod("Execute", BindingFlags.Public | BindingFlags.Instance), this);

什么是问题?更重要的是,我能以MVVM友好的方式解决它吗?提前谢谢!

  <ListView Name="TestListe1" ItemsSource="{Binding TestCollection, UpdateSourceTrigger=PropertyChanged}" Margin="68,22,620,8" IsSynchronizedWithCurrentItem="True" FontSize="12">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Width="{Binding TmpWdth,ElementName=ThisPage}" >
                                        <GridViewColumn.HeaderTemplate>
                                    <DataTemplate    >
                                        <TextBlock Text="ST1" Margin="10,0,10,1" >              
                                        </TextBlock>
                                       </DataTemplate>
                                </GridViewColumn.HeaderTemplate>
                             <GridViewColumn.CellTemplate>
                                    <DataTemplate  DataType="{x:Type local:TestListItem}" >
                                        <TextBlock Text="{Binding Element_1, Mode=OneWay}" >
                                            <TextBlock.InputBindings>
                                                 <MouseBinding Gesture="LeftDoubleClick" Command="{Binding  Openusercontrolcommand}"/>
                                            </TextBlock.InputBindings>
                                        </TextBlock>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                            <GridViewColumn Width="Auto">
                                <GridViewColumn.HeaderTemplate>
                                    <DataTemplate DataType="{x:Type local:TestListItem}" >
                                        <TextBlock Text="T2" Margin="10,0,10,0" >
                                       </TextBlock>
                                    </DataTemplate>
                                </GridViewColumn.HeaderTemplate>
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Element_2, Mode=OneWay}"  >
                                    <TextBlock.InputBindings>
                                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MyDoubleClickCommand}"/>
                                    </TextBlock.InputBindings>
                                    </TextBlock>                                   
                                   </DataTemplate>
                                </GridViewColumn.CellTemplate>
                                </GridViewColumn>

1 个答案:

答案 0 :(得分:2)

如果您只想处理双击,可以使用UIElement.InputBindings

    <ListView ItemsSource="{Binding Items}">
        <ListView.InputBindings>
            <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MyListViewCommand}"/>
        </ListView.InputBindings>
        <ListView.ItemTemplate>
            <DataTemplate DataType="{x:Type local:Item}">
                <TextBlock Text="{Binding Text}">
                    <TextBlock.InputBindings>
                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MyItemCommand}"/>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>            
    </ListView>