In WPF app I have a ListView which is connected with ObservableCollection ShQuCollection through databinding:
<ListView Name="ShSelList" ItemsSource="{Binding Source={StaticResource myDataSource},Path=ShQuCollection}" SelectionChanged="ShSelList_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Code" DisplayMemberBinding="{Binding StrCode}"/>
<GridViewColumn Header="Date" DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Time" DisplayMemberBinding="{Binding Time}"/>
</GridView>
</ListView.View>
</ListView>
From inside ListView SelectionChanged event handler I need to call a method and pass to it a string parameter, taking it from one of the field of the selected row of the ObservableCollection ShQuCollection.
How I could reference the ObservableCollection from inside ListView SelectionChanged event handler?
private void ShSelList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
...?????
}
Edited (added):
My ObservableCollection is in code-behind file of another window and I use Window.Resources declaration to reach it.
<Window.Resources>
<c:ShWindow x:Key="myDataSource"/>
</Window.Resources>
And ObservableCollection looks like:
ObservableCollection<ShsQu> _ShQuCollection =
new ObservableCollection<ShsQu>();
public ObservableCollection<ShsQu> ShQuCollection
{ get { return _ShQuCollection; } }
public class ShsQu
{
public string StrCode { get; set; }
public string Date { get; set; }
public string Time { get; set; }
}
I am assuming your ModelView is attached to your View. Meaning ShQuCollection should be a public property within your ModelView. You should just have to access the
ObservableCollectionthrough your ModelView.Update:
To Reach the record in which you need to modify you grab the current selectedIndex from your listView.
Note: It would be cleaner in the future to use the MVVM approach.