Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf listview item object doubleclick

I have a list of objects that get dynamically created when the window opens. For instance:

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

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

When an item in the list is doubleclicked, I simply want to open a new window. Object gets passed to the new window, where I do something with it. Any simple solution?

like image 597
user3117628 Avatar asked Jul 17 '26 23:07

user3117628


2 Answers

Use ListView's MouseDoubleClick.

XAML:

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

Code Behind:

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);
        }
}
like image 104
Sajeetharan Avatar answered Jul 20 '26 16:07

Sajeetharan


Try this out...

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();
}
like image 20
John Stritenberger Avatar answered Jul 20 '26 16:07

John Stritenberger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!