Wpf 列表视图项目对象双击

Wpf listview item object doubleclick

我有一个在 window 打开时动态创建的对象列表。例如:

//Set content for listview sentitems
inbox.ItemsSource = from email in _dataDC.emails
                    where email.from == _username
                    orderby email.time descending
                    select email;

我的xaml:

<TabItem Header="Inbox" Height="30">
    <TabItem.Content>
        <ListView Name="inbox" BorderThickness="2" Margin="5,0,-5,0">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Van" Width="70" DisplayMemberBinding="{Binding from}" />
                    <GridViewColumn Header="Onderwerp" Width="120" DisplayMemberBinding="{Binding subject}" />
                    <GridViewColumn Header="Op" Width="130" DisplayMemberBinding="{Binding time}" />
                </GridView>
            </ListView.View>
        </ListView>
    </TabItem.Content>
</TabItem>

当双击列表中的项目时,我只想打开一个新的 window。对象被传递到新的 window,在那里我用它做了一些事情。有什么简单的解决办法吗?

使用 ListView 的 MouseDoubleClick。

XAML:

<ListView Name="inbox" BorderThickness="2" Margin="5,0,-5,0" MouseDoubleClick="ListView_MouseDoubleClick"> 

隐藏代码:

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
       var item = (sender as ListView).SelectedItem;
        if (item != null)
        {
            //use the item here and pass to the new window
            NewModal s = new NewModal(Email)item);
        }
}

试试这个...

XAML

<ListView  Name="inbox" BorderThickness="2" Margin="5,0,-5,0" MouseDoubleClick="inbox_OnMouseDoubleClick">

C#

private void inbox_OnMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // Assumes your NewWindow class has a constuctor that takes the Email type.
    NewWindow window = new NewWindow((Email)inbox.SelectedItem);
    window.Show();
}