如何从 WPF ListView SelectionChanged 事件处理程序引用 ObservableCollection?

发布于 2024-08-17 05:14:07 字数 1676 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水波映月 2024-08-24 05:14:07

我假设您的 ModelView 已附加到您的视图。这意味着 ShQuCollection 应该是 ModelView 中的公共属性。您只需通过 ModelView 访问 ObservableCollection 即可。

更新:

要到达需要修改的记录,请从列表视图中获取当前的 selectedIndex。

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

注意:将来使用 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.

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.

定格我的天空 2024-08-24 05:14:07

在后面的代码中,您应该能够将列表视图 (SsSelList) 的选定项属性转换为 ShsQu 对象,并访问该对象的属性以调用您的方法。

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

这应该可以工作,但是这不是一个非常干净的方法,我建议使用 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文