当 ListView 的 ItemsSource 更改时触发事件

发布于 2024-12-18 02:13:16 字数 1675 浏览 1 评论 0原文

我找不到可以在加载窗口后调用的事件,也找不到可以访问 ListView 的 ItemsSource 的事件。我唯一能想到的是 ListView 中的 Loaded 事件,但是当该事件被触发时,ItemsSource 保持为空。

我可能需要另一个事件,以便我可以知道 ItemsSource 中的内容。

因此,通过代码,我可能会更好地公开我正在尝试做的事情:

在自定义类中:

public class GridViewSomething
{
    public static readonly DependencyProperty TestProperty =
        DependencyProperty.RegisterAttached("Test",
        typeof(bool),
        typeof(GridViewSomething),
        new UIPropertyMetadata(OnTestChanged));

    public static bool GetTest(DependencyObject obj)
    {
        return (bool)obj.GetValue(TestProperty);
    }

    public static void SetTest(DependencyObject obj, bool value)
    {
        obj.SetValue(TestProperty, value);
    }

    static void OnTestChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        ListView listView = sender as ListView;

        if (!(bool)e.OldValue && (bool)e.NewValue)
            listView.AddHandler(ListView.LoadedEvent, new RoutedEventHandler(ListView_Loaded));
        else if (!(bool)e.NewValue && (bool)e.OldValue)
            listView.RemoveHandler(ListView.LoadedEvent, new RoutedEventHandler(ListView_Loaded);
    }

    static void ListView_Loaded(object sender, RoutedEventArgs e)
    {
        ListView listView = sender as ListView;

        if (listView.ItemsSource != null)
        {
            //Do some work
        }
    }
}

和ListView:

(...)

<ListView ItemsSource="{Binding Students}"
          test:GridViewSomething.Test="True">

(...)

我将ListView绑定到此窗口的ViewModel中的集合。我需要准确地知道该自定义类的 ItemsSource 中的内容。

那么我怎样才能实现这一目标呢?

提前致谢!

I can't find an event that can be called after a window was loaded, and where i can get access to the ItemsSource of a ListView. The only thing i can think is the Loaded event in the ListView but when this event is fired the ItemsSource remains null.

I probably need another event so i can know what is in the ItemsSource.

So with code i will probably expose better what i am trying to do:

In a custom class:

public class GridViewSomething
{
    public static readonly DependencyProperty TestProperty =
        DependencyProperty.RegisterAttached("Test",
        typeof(bool),
        typeof(GridViewSomething),
        new UIPropertyMetadata(OnTestChanged));

    public static bool GetTest(DependencyObject obj)
    {
        return (bool)obj.GetValue(TestProperty);
    }

    public static void SetTest(DependencyObject obj, bool value)
    {
        obj.SetValue(TestProperty, value);
    }

    static void OnTestChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        ListView listView = sender as ListView;

        if (!(bool)e.OldValue && (bool)e.NewValue)
            listView.AddHandler(ListView.LoadedEvent, new RoutedEventHandler(ListView_Loaded));
        else if (!(bool)e.NewValue && (bool)e.OldValue)
            listView.RemoveHandler(ListView.LoadedEvent, new RoutedEventHandler(ListView_Loaded);
    }

    static void ListView_Loaded(object sender, RoutedEventArgs e)
    {
        ListView listView = sender as ListView;

        if (listView.ItemsSource != null)
        {
            //Do some work
        }
    }
}

And the ListView:

(...)

<ListView ItemsSource="{Binding Students}"
          test:GridViewSomething.Test="True">

(...)

I am binding the ListView to a Collection in the ViewModel of this Window. I need to know precisely what is in the ItemsSource in that custom class.

So how i can achieve this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

与风相奔跑 2024-12-25 02:13:16

您可以使用描述符 如此处所示。如果检查该处理程序中的值,它不应为 null(除非绑定属性实际上设置为 null)。

You could subsribe to changes on the ItemsSource property by using a descriptor as shown here. If check the value in that handler it should not be null (unless the bound property was in fact set to null).

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