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?
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);
}
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With