2

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!

Askolein
  • 3,250
  • 3
  • 28
  • 40
Miguel
  • 907
  • 4
  • 26
  • 50
  • You already know that the bound colection is the ItemsSource, why would you need it to be in the view to do something? – H.B. Nov 27 '11 at 22:23
  • What i am trying to do is auto generate the columns based on the bound collection to the ListView... It's why i am tring to do this way. I don't have many experience with DependencyProperties and this a first idea, and it's why i am trying to find the appropriated event to be called that look into an array of strings in the collection (through the ItemsSource) and add a GridView with the Columns to the ListView. – Miguel Nov 27 '11 at 22:30
  • Try out overriding OnApplyTemplate, but why you need that perhaps solution in an other place? – sll Nov 27 '11 at 22:41

1 Answers1

2

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).

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • You mean the DependencyPropertyDescriptor? I am a bit confused now, how it works and where it should be applied? – Miguel Nov 27 '11 at 23:21
  • @Miguel: You can subscribe in the constructor of your window for example, or in the loaded event of the ListView. – H.B. Nov 27 '11 at 23:38
  • It worked. Thanks! I started really recently working with DependencyProperties so i don't have any experience using them or know what we have to do the stuff we need but they are really useful and helpful. – Miguel Nov 28 '11 at 00:59