C# 如何使双击“编辑”在列表视图中处理一行?
我有一个简单的列表视图和gridview来显示每一行 我为delete添加了一个键绑定,它工作得很好C# 如何使双击“编辑”在列表视图中处理一行?,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个简单的列表视图和gridview来显示每一行 我为delete添加了一个键绑定,它工作得很好 <ListView.InputBindings> <KeyBinding Key="Delete" Command="{Binding Path=DeleteKeyCommand}" CommandParameter="{Binding ElementName=DatabasesLstVw, Path=SelectedItem}"/> </ListV
<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>
由于缺少一些代码,因此应该是这样的:
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>
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
}
在viewModel中,命令定义如下:
<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>
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
}
RelayCommand\u项目双击;
公共i命令项双击
{
得到
{
如果(_ItemDoubleClick==null)
{
_ItemDoubleClick=新建RelayCommand(this.ItemDoubleClickExecuted,
param=>this.ItemDoubleClickCanExecute());
}
返回_ItemDoubleClick;
}
}
私有布尔项DoubleClickCanExecute()
{
返回true;
}
私有无效项DoubleClickExecuted(字符串项)
{
//在item中,您已获得双击ListViewItem的文本
}
请注意,在此示例中,绑定的ListViewObservableCollection
是string类型。如果这是其他类型,则应更改ICommand定义中的类型。别忘了将WindowDataContext设置为ViewModel。
希望这一点现在更清楚。由于缺少一些代码,应该是这样的:
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>
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
}
在viewModel中,命令定义如下:
<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>
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
}
RelayCommand\u项目双击;
公共i命令项双击
{
得到
{
如果(_ItemDoubleClick==null)
{
_ItemDoubleClick=新建RelayCommand(this.ItemDoubleClickExecuted,
param=>this.ItemDoubleClickCanExecute());
}
返回_ItemDoubleClick;
}
}
私有布尔项DoubleClickCanExecute()
{
返回true;
}
私有无效项DoubleClickExecuted(字符串项)
{
//在item中,您已获得双击ListViewItem的文本
}
请注意,在此示例中,绑定的ListViewObservableCollection
是string类型。如果这是其他类型,则应更改ICommand定义中的类型。别忘了将WindowDataContext设置为ViewModel。
希望现在更清楚。您已经在列表视图上添加了双击,而不是列表项。在您的代码隐藏中,将其添加到每个项目中。可能重复@Variable我很确定它在列表视图中不是该项目。@Pikoh我已经尝试过了它,但它没有说明local:是什么,因此它无法正常工作。很不幸,local显然是您的项目名称空间您已在列表视图中添加了双击,而不是列表项目。在您的代码隐藏中,将其添加到每个项目中。可能重复@Variable我很确定它在列表视图中不是该项目。@Pikoh我已经尝试过了它,但它没有说明本地:是什么,因此它无法正常工作。很不幸,本地显然是您的项目名称空间我会测试它,如果它有效,我将为您提供帮助nub的奖金:)我希望如此作品如果不行,就告诉我的朋友,我自己试试。而且赏金不是必需的,别担心:)检查问题第二个答案是互动有效的。我有一些巨大的调试问题与您的。类型“AddToInputBinding”初始化失败:“MouseDoubleClickListviewItem.Util.AddToInputBinding”的类型初始值设定项引发异常。那有什么区别?如果我用另一个有关系吗?不,真的没关系。如果第二种方法对你有效,不要回头看,两者都可以:)我可能会继续尝试,只是因为我想了解依赖财产是如何运作的。我会测试它是否有效,我会给你一笔帮助一个裸体的赏金:)我希望它有效。如果不行,就告诉我的朋友,我自己试试。而且赏金不是必需的,别担心:)检查问题第二个答案是互动有效的。我有一些巨大的调试问题与您的。类型“AddToInputBinding”初始化失败:“MouseDoubleClickListviewItem.Util.AddToInputBinding”的类型初始值设定项引发异常。那有什么区别?如果我用另一个有关系吗?不,真的没关系。如果第二个对你有效,不要回头看,两者都可以:)我可能会继续尝试这个,因为我想了解DependencyProperty是如何工作的。