【问题标题】:using Attached Commands to fire a double click event for listview - MVVM使用附加命令触发列表视图的双击事件 - MVVM
【发布时间】: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


    【解决方案1】:

    如果你只想处理双击,你可以使用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>
    

    【讨论】:

    • 谢谢你的回答,但是这个解决方案可以兼容 MVVM 吗?我编辑了上面的描述!
    • 可以肯定,因为您的虚拟机现在能够处理 DoubleClick,您的代码没有虚拟机逻辑,并且您的虚拟机没有任何视觉元素。
    • 它仅适用于列表视图,这意味着当我单击列表视图内部而不是元素时!对于文本块它不起作用,你能告诉哪个元素被定义为“local:Item”吗?提前致谢
    • 我的 ItemsSource 是一个 ObservableCollection 在这种情况下它应该是一个 ViewModel 但它取决于您的需要,如果 ListView 上的双击可以满足您的需求,您不需要其他东西但是如果您还需要在用户点击 ListViewItem 时触发,您还必须在此项目中添加命令
    • 是 genau funktioniert nicht 吗? ich weiss jetzt nicht ob dir das bewusst ist aber es sind 2 verschiedene Klasse das eine welches die Items und den MyListViewCommand beinhaltet und die Klasse welche deine ListboxItems 代表 meinem bsp Text und halte @987654 hängt von der Logik ab welche du anbindest editor mal deine Frage mit deinem aktuellen Code vielleicht sehe ich den Fehler
    猜你喜欢
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 2018-03-31
    • 2012-09-28
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多