开发者

How to reference ObservableCollection from WPF ListView SelectionChanged event handler?

开发者 https://www.devze.com 2022-12-15 17:06 出处:网络
In WPF app I have a ListView which is connected with ObservableCo开发者_C百科llectionShQuCollection through databinding:

In WPF app I have a ListView which is connected with ObservableCo开发者_C百科llection 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 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号