Reputation: 241
C#
WPF
I have a list view with 3 columns in it. What I want to happen is when I double click on a row, it will capture the values of the columns for the selected row and store them as variables so I can pass them off to another page or simply manipulate them as needed. What I have here is my practice code. For right now, I am just populating a message box to see if I can get the variables out correctly. What is happening is it is not grabbing anything from the row. I figured this out by removing the if
block and saw that string name = selectedObject.ilName;
was null. an additional question I have is in regards to the statement ingListLV.SelectedItems[0] as InventoryList
the [0], what exactly does that refer to? I initally thought it referred to the values it returned, so [0] would be the value in column 1, [1] would be column 2 etc. but i know that is wrong.
Here is my XAML
<ListView x:Name="ingListLV" HorizontalAlignment="Center" Height="100" Margin="0,145,0,0" VerticalAlignment="Top" Width="Auto"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
SelectedValuePath="InventoryName"
Style="{DynamicResource listViewStyle}"
MouseDoubleClick="Open_Edit">
<ListView.View>
<GridView>
<GridViewColumn x:Name="InvName" Width="100" Header="Name" DisplayMemberBinding="{Binding Path=InventoryName}" />
<GridViewColumn Width="50" Header="Qty" DisplayMemberBinding="{Binding Path=Qty}" />
<GridViewColumn Width="50" Header="Type" DisplayMemberBinding="{Binding Path=Type}" />
</GridView>
</ListView.View>
</ListView>
and my code behind
private void Open_Edit(object sender, RoutedEventArgs e)
{
var selectedObject = ingListLV.SelectedItems[0] as InventoryList;
if (selectedObject == null)
{
return;
}
string name = selectedObject.ilName;
MessageBox.Show(name);
}
public class InventoryList
{
public string ilName { get; set; }
public decimal ilQty { get; set; }
public string ilType { get; set; }
}
EDIT Here is where i am loading data in to the listview
private void LoadLV()
{
auroraDataEntities = new AuroraDataEntities();
ObjectQuery<Inventory> inventories = auroraDataEntities.Inventories;
//Returns only opjects with a quantity greater than 0, so it won't show anything you are out of
var fillList = from q in inventories
where q.Qty > 0
select q;
ingListLV.ItemsSource = fillList.ToList();
}
Upvotes: 0
Views: 721
Reputation: 17083
In ListView.SelectionMode
Single
(which is default) use SelectedItem not SelectedItems.
var selectedObject = ingListLV.SelectedItem as Inventory;
The [0]
refers to the first selected item (row) in a multiple selection.
Upvotes: 1