我正在使用附加命令为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>
答案 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>