如何从 WPF ListView SelectionChanged 事件处理程序引用 ObservableCollection?
在 WPF 应用程序中,我有一个 ListView,它通过数据绑定与 ObservableCollection ShQuCollection
连接:
<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>
从 ListView SelectionChanged 事件处理程序内部,我需要调用一个方法并向其传递一个字符串参数,从其中一个字段获取它ObservableCollection ShQuCollection
的选定行的位置。
如何从 ListView SelectionChanged 事件处理程序内部引用 ObservableCollection?
private void ShSelList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
...?????
}
编辑(添加):
我的 ObservableCollection 位于另一个窗口的代码隐藏文件中,我使用 Window.Resources 声明来访问它。
<Window.Resources>
<c:ShWindow x:Key="myDataSource"/>
</Window.Resources>
ObservableCollection 看起来像:
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; }
}
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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您的 ModelView 已附加到您的视图。这意味着 ShQuCollection 应该是 ModelView 中的公共属性。您只需通过 ModelView 访问
ObservableCollection
即可。更新:
要到达需要修改的记录,请从列表视图中获取当前的 selectedIndex。
注意:将来使用 MVVM 方法会更简洁。
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.
Note: It would be cleaner in the future to use the MVVM approach.
在后面的代码中,您应该能够将列表视图 (SsSelList) 的选定项属性转换为 ShsQu 对象,并访问该对象的属性以调用您的方法。
这应该可以工作,但是这不是一个非常干净的方法,我建议使用 MVVM 模式。如果您使用 MVVM,您会将集合存储在视图模型中并跟踪视图模型中的当前项目。这样,在视图模型中发出的任何命令都可以访问当前项目。
Josh Smith 在这里给出了很好的介绍 (http://msdn.microsoft.com/如果您有兴趣进一步阅读,请转至 MVVM(en-us/magazine/dd419663.aspx)。
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.
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.