为了引用listview控件的双击事件,我需要做什么?
答案 0 :(得分:62)
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
</Style>
</ListView.ItemContainerStyle>
唯一的困难是,如果您对listviewitem映射到的基础对象感兴趣,例如
private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ListViewItem item = sender as ListViewItem;
object obj = item.Content;
}
答案 1 :(得分:36)
我正在使用类似这样的东西,只在双击ListViewItem时触发,而不是双击ListView的标题时。
private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DependencyObject obj = (DependencyObject)e.OriginalSource;
while (obj != null && obj != myListView)
{
if (obj.GetType() == typeof(ListViewItem))
{
// Do something here
MessageBox.Show("A ListViewItem was double clicked!");
break;
}
obj = VisualTreeHelper.GetParent(obj);
}
}
答案 2 :(得分:8)
private void positionsListView_DoubleClick(object sender, EventArgs e)
{
if (positionsListView.SelectedItems.Count == 1)
{
ListView.SelectedListViewItemCollection items = positionsListView.SelectedItems;
ListViewItem lvItem = items[0];
string what = lvItem.Text;
}
}
答案 3 :(得分:7)
使用ListView.HitTest
方法
private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
{
var senderList = (ListView) sender;
var clickedItem = senderList.HitTest(e.Location).Item;
if (clickedItem != null)
{
//do something
}
}
或旧方式
private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
{
var senderList = (ListView) sender;
if (senderList.SelectedItems.Count == 1 && IsInBound(e.Location, senderList.SelectedItems[0].Bounds))
{
//Do something
}
}
public bool IsInBound(Point location, Rectangle bound)
{
return (bound.Y <= location.Y &&
bound.Y + bound.Height >= location.Y &&
bound.X <= location.X &&
bound.X + bound.Width >= location.X);
}
答案 4 :(得分:5)
我也需要它。我发现在msdn上:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.activation.aspx
我认为这个代表就是为了这个。
答案 5 :(得分:4)
我还没有足够大的声誉分数来添加评论,这将是最有帮助的,但这与那些询问.Net 4.5解决方案有关。
您可以使用鼠标X和Y坐标以及ListView方法GetItemAt来查找已单击的项目。
private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ListViewItem item = myListView.GetItemAt(e.X, e.Y)
// Do something here
}
答案 6 :(得分:3)
对我来说,我在这段代码中双击ListView。
this.listView.Activation = ItemActivation.TwoClick;
this.listView.ItemActivate += ListView1_ItemActivate;
ItemActivate specify how user activate with items
当用户双击时,将触发ListView1_ItemActivate。 ListView ItemActivate的属性是指访问所选项目的集合。
private void ListView1_ItemActivate(Object sender, EventArgs e)
{
foreach (ListViewItem item in listView.SelectedItems)
//do something
}
它对我有用。
答案 7 :(得分:1)
我在Microsoft Dev Center上找到了这个。它工作正常,忽略在错误的地方双击。如您所见,重点是在触发双击事件之前选择一个项目。
private void listView1_DoubleClick(object sender, EventArgs e)
{
// user clicked an item of listview control
if (listView1.SelectedItems.Count == 1)
{
//do what you need to do here
}
}
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/588b1053-8a8f-44ab-8b44-2e42062fb663
答案 8 :(得分:1)
以下是如何在WPF列表视图中获取双击列表视图项的选定对象和对象匹配代码:
/// <summary>
/// Get the object from the selected listview item.
/// </summary>
/// <param name="LV"></param>
/// <param name="originalSource"></param>
/// <returns></returns>
private object GetListViewItemObject(ListView LV, object originalSource)
{
DependencyObject dep = (DependencyObject)originalSource;
while ((dep != null) && !(dep.GetType() == typeof(ListViewItem)))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return null;
object obj = (Object)LV.ItemContainerGenerator.ItemFromContainer(dep);
return obj;
}
private void lvFiles_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
object obj = GetListViewItemObject(lvFiles, e.OriginalSource);
if (obj.GetType() == typeof(MyObject))
{
MyObject MyObject = (MyObject)obj;
// Add the rest of your logic here.
}
}
答案 9 :(得分:1)
您可以先获取ListView,然后获取Selected ListViewItem。 我有一个ListBox的例子,但ListView应该是类似的。
private void listBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ListBox box = sender as ListBox;
if (box == null) {
return;
}
MyInfo info = box.SelectedItem as MyInfo;
if (info == null)
return;
/* your code here */
}
e.Handled = true;
}
答案 10 :(得分:0)
在ListBox DoubleClick事件中,获取列表框的selecteditem(s)成员,然后就可以了。
void ListBox1DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(string.Format("SelectedItem:\n{0}",listBox1.SelectedItem.ToString()));
}
答案 11 :(得分:0)
使用MouseDoubleClick事件,并且所有MouseClick事件在eventargs变量'e'中都有一个点击计数。因此,如果e.ClickCount == 2,则双击。
答案 12 :(得分:0)
这很烦人,但最好的办法就是:
<DataTemplate Name="MyCoolDataTemplate">
<Grid Loaded="HookLVIClicked" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
<!-- your code here -->
</Grid>
</DataTemplate>
然后在代码中:
public void HookLVIClicked(object sender, RoutedEventArgs e) {
var fe = (FrameworkElement)sender;
var lvi = (ListViewItem)fe.Tag;
lvi.MouseDoubleClick += MyMouseDoubleClickHandler;
}
答案 13 :(得分:0)
与ListBox有类似的问题想要打开一个窗口(不同的视图),其中SelectedItem作为上下文(在我的情况下,所以我可以编辑它)。
我发现的三个选项是:1。代码背后2.使用附加行为3.使用Blend的i:使用MVVM-Light进行交互和EventToCommand。
我选择了第3个选项,它看起来像这样:
<ListBox x:Name="You_Need_This_Name"
ItemsSource="{Binding Your_Collection_Name_Here}"
SelectedItem="{Binding Your_Property_Name_Here, UpdateSourceTrigger=PropertyChanged}"
... rest of your needed stuff here ...
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<Command:EventToCommand Command="{Binding Your_Command_Name_Here}"
CommandParameter="{Binding ElementName=You_Need_This_Name,Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
就是这样...当你双击你想要的项目时,你将使用SelectedItem作为参数调用ViewModel上的方法,你可以在那里做任何你想做的事情:)
答案 14 :(得分:0)
发件人的类型为ListView而不是ListViewItem。
private void listViewTriggers_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListView triggerView = sender as ListView;
if (triggerView != null)
{
btnEditTrigger_Click(null, null);
}
}
答案 15 :(得分:-1)
我认为这个主题在谷歌上很高,有我简单而有效的样本:)
XAML:
<ListView Name="MainTCList" HorizontalAlignment="Stretch" MinHeight="440" Height="Auto" Margin="10,10,5.115,4" VerticalAlignment="Stretch" MinWidth="500" Width="Auto" Grid.Column="0" MouseDoubleClick="MainTCList_MouseDoubleClick" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="UserTID" DisplayMemberBinding="{Binding UserTID}" Width="80"/>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" Width="410" />
</GridView>
</ListView.View>
</ListView>
C#
private void MainTCList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TC item = (TC)MainTCList.Items.CurrentItem;
Wyswietlacz.Content = item.UserTID;
}
Wyswietlacz是一个测试标签,用于查看项目内容:)我在这最后一行添加了一个方法来加载包含项目数据的页面。