【发布时间】:2014-01-09 09:08:43
【问题描述】:
我使用 Attached 命令为 Listview 中的项目触发双击事件,我使用了 marlon grechs 博客中编写的方法 - (AttachedCommandBehavior V2 aka ACB),因为我使用的是 MVVM!
我在一个小型测试示例中使用它并且工作正常,但问题是,当我将它集成到应用程序中时,每次打开页面时我都会遇到异常/错误,其中使用了附加属性。
异常说:
Invalid Operation Exception - Could not resolve event name
{Void BindEvent(System.Windows.DependencyObject, System.String)}
它指向事件处理程序定义
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>
【问题讨论】:
标签: wpf listview mvvm attached-properties