Wpf列表视图项目对象双击

3

我有一个对象列表,在窗口打开时会动态创建。例如:

//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>

当列表中的项目被双击时,我只想打开一个新窗口。将对象传递给新窗口,在那里我会对其进行操作。有简单的解决方案吗?

2个回答

2
使用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);
        }
}

这只需要单击,是吗?它必须是双击才行 :3 - user3117628
@user3227070,是的,你可以有。 - Sajeetharan
抱歉,出现错误,无法将类型从对象转换为电子邮件。我会编辑我的问题,稍等片刻。 - user3117628
在调用NewModal构造函数时,将item转换为Email类型。例如:NewModal s = new NewModal((Email)item); - John Stritenberger
@user3227070,正如gengis所提到的,您需要进行类型转换。 - Sajeetharan

1

试试这个...

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();
}

这里与我发布的答案有什么不同? - Sajeetharan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接