views:

971

answers:

2

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; }
    }
+1  A: 

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 ObservableCollection through your ModelView.

Update:

To Reach the record in which you need to modify you grab the current selectedIndex from your listView.

private void ShSelList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   string s = ShQuCollection[ShSelList.SelectedIndex].StrCode;
}

Note: It would be cleaner in the future to use the MVVM approach.

jsmith
unfortunately, I have not yet enough learned MVVM and my collection is in code-behind file of the window. Is there a way out in this case?
rem
Can you update your question to include the code of where you are declaring your ObservableCollection?
jsmith
I have included in my updated question declaring of my collection. And one thing to note: I need not simply referense my collection from event handler but reach that record wnich now is selected after SelectionChanged.
rem
Yes, it works. I had just to add a reference to window and assigh result to a string variable: "string str = sw.ShQuCollection[ShSelList.SelectedIndex].StrCode;" and then use "str" as a parameter. Thanks a lot! And I promise to use MVVM in the future, ..when I get ready :)
rem
A: 

In your code behind you should be able to cast the selected item property of the listview (SsSelList) to a ShsQu object and access the properties of that object to call your method.

ShSQu obj = SsSelList.SelectedItem  as ShSQu;
// Then call the method using the object properties
MethodToCall(obj.StrCode);

This should work however is not a very clean way of doing this and I would recommend using the MVVM pattern. If you were using MVVM you would store your collection in the viewmodel and keep track of the current item in the viewmodel. This way any command that is raised in the viewmodel can access the current item.

Josh Smith gives a good introduction here (http://msdn.microsoft.com/en-us/magazine/dd419663.aspx) to MVVM if your interested in reading further.

mattythomas2000
Thank you for the link, I will for sure learn this important thing. Unfortunately I can't do casting in accordance with your example. It just doesn't compile in my code, maybe something missing
rem
Sorry that was silly of me, object is a keyword and shouldn't be used for a variable name change the variable name object to obj
mattythomas2000