如何在列表视图中的一行上进行双击编辑?

时间:2016-05-02 13:06:44

标签: c# wpf mvvm

我有一个带有gridview的简单列表视图来显示每一行。

我添加了一个用于删除的键绑定,它正常工作。

 <ListView.InputBindings>
      <KeyBinding Key="Delete" Command="{Binding Path=DeleteKeyCommand}" CommandParameter="{Binding ElementName=DatabasesLstVw, Path=SelectedItem}"/>
 </ListView.InputBindings>

但是当我为LeftDoubleClick添加一个Mousebinding来编辑它没有触发命令时。

 <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Path=LeftDoubleClickCommand}" CommandParameter="{Binding ElementName=DatabasesLstVw, Path=SelectedItem}" />

花了最后两个小时试图搞清楚我唯一想到的是它点击整个列表视图而不是listview项目???

如何在列表视图中的一行上进行双击编辑?我正在使用MVVM我不想打破它,所以我不能使用代码来破解它。必须有一种方法将命令映射回我的视图模型。

更新更多代码:

 <ListView x:Name="DatabasesLstVw" ItemsSource="{Binding Path=ClientDetails.Databases}" ItemContainerStyle="{StaticResource alternatingStyle}" AlternationCount="2" Grid.Row="2" Grid.ColumnSpan="4" VerticalAlignment="Top" >                
               <ListView.InputBindings>
                    <KeyBinding Key="Delete" Command="{Binding Path=DeleteKeyCommand}" CommandParameter="{Binding ElementName=DatabasesLstVw, Path=SelectedItem}"/>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Path=LeftDoubleClickCommand}" CommandParameter="{Binding ElementName=DatabasesLstVw, Path=SelectedItem}" />
                </ListView.InputBindings>

1 个答案:

答案 0 :(得分:3)

由于referenced answer缺少某些代码,因此应该如此:

    public class AddToInputBinding
    {
        public static System.Windows.Input.InputBinding GetBinding(DependencyObject obj)
        {
            return (System.Windows.Input.InputBinding)obj.GetValue(BindingProp);
        }

        public static void SetBinding(DependencyObject obj, System.Windows.Input.InputBinding value)
        {
            obj.SetValue(BindingProp, value);
        }


        public static readonly DependencyProperty BindingProp = DependencyProperty.RegisterAttached(
          "Binding", typeof(System.Windows.Input.InputBinding), typeof(AddToInputBinding), new PropertyMetadata
          {
              PropertyChangedCallback = (obj, e) =>
              {
                  ((UIElement)obj).InputBindings.Add((System.Windows.Input.InputBinding)e.NewValue);
              }
          });
    }

然后,在你的XAML中,你会做这样的事情:

<Window x:Class="WpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication"
    Title="Window1" Height="300" Width="300">
<Window.Resources>
    <ResourceDictionary>
        <Style TargetType="ListViewItem">
            <Setter Property="local:AddToInputBinding.Binding">
                <Setter.Value>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DataContext.ItemDoubleClick,
                                                                                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
                                                                                    CommandParameter="{Binding}"/>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ListView ItemsSource="{Binding Patients}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Test" />
            </GridView>

        </ListView.View>
    </ListView>

</Grid>

在viewModel中,命令定义如下:

    RelayCommand<string> _ItemDoubleClick;
    public ICommand ItemDoubleClick
    {
        get
        {
            if (_ItemDoubleClick == null)
            {
                _ItemDoubleClick = new RelayCommand<string>(this.ItemDoubleClickExecuted,
                    param => this.ItemDoubleClickCanExecute());

            }
            return _ItemDoubleClick;
        }
    }

    private bool ItemDoubleClickCanExecute()
    {
        return true;
    }

    private void ItemDoubleClickExecuted(string item)
    {
        //In item you've got the text of double clicked ListViewItem
    }

请注意,在此示例中,ListView绑定ObservableCollection的类型为字符串。如果这是其他类型,则应更改ICommand定义中的类型。不要忘记将Window DataContext设置为ViewModel。 希望现在更清楚了。