none
ListView - ItemsSource Changed Event? RRS feed

  • Question

  • Hi,

    Is it possible to capture when the ItemsSource property on a ListView is changed?
    I would like to add an adorner to my items in the ListView, however I can't determine when the collection property is changed. Because I would like to add an adorner, this event needs to be captured in code.

    Thanking you,
    -Brad
    Friday, July 4, 2008 5:02 AM

Answers

  • You can use the below code to capture when any dependency property is changed. I'm not sure if it'll solve your particular issue in this instance, though.

    TypeDescriptor.GetProperties(object)["PropertyName"].AddValueChanged();

    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Brad Leach Sunday, July 6, 2008 11:55 AM
    Friday, July 4, 2008 10:31 AM
  •    


    I guess the problem it that you pass this as the first Parameter into method AddValueChanged()

    pls try the following code, that should help you
      TypeDescriptor.GetProperties(listviewcontrol)["ItemsSource"].AddValueChanged(listviewcontrol, new EventHandler(ListView_ItemsSourceChanged));

    I guess 4 your situation, code should be 

    TypeDescriptor.GetProperties(this.SearchResults)["ItemsSource"].AddValueChanged(this.SearchResultsnew EventHandler(ListView_ItemsSourceChanged)); 

    hope that helps


    珍惜生命,远离美女
    • Marked as answer by Brad Leach Sunday, July 6, 2008 11:55 AM
    Saturday, July 5, 2008 10:31 AM

All replies

  • You can use the below code to capture when any dependency property is changed. I'm not sure if it'll solve your particular issue in this instance, though.

    TypeDescriptor.GetProperties(object)["PropertyName"].AddValueChanged();

    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    • Marked as answer by Brad Leach Sunday, July 6, 2008 11:55 AM
    Friday, July 4, 2008 10:31 AM
  • You could use ObservableCollection<T> as an ItemsSource for your listview. It provides a very usefull set of events for tracking collection changes.
    TypeDescriptor scenario is also possible but you will pay performance for that and will need to remove event hanlders assigned for the property during disposal for not to introduce memory leaks.
    Kind regards, Denis (GMT +02:00)
    • Proposed as answer by Denis Vuyka Friday, July 4, 2008 3:40 PM
    • Unproposed as answer by Brad Leach Friday, July 4, 2008 9:34 PM
    Friday, July 4, 2008 3:40 PM
  • Denis Vuyka said:

    You could use ObservableCollection<T> as an ItemsSource for your listview.

    ---


    I'm sorry - I wasn't clear enough in my first post. I am not interested when an item inside a collection changes - I am interested only when the ItemsSource property of the ListView is hooked up to a new collection.

    I probably need to add some more information about my scenario. I have a text box which has some user supplied search text. The search results are displayed in a ListView. I want to highlight the search terms when the search results are bound to the ListView.

    I am using an adorner layer to render the "highlighting". I have the appropriate text highlighted by the adorner in a test textbox, but I'm struggling in getting the adorner to display on the ListView. One of the reasons for this is that I currently have no way of determining when the ListView's ItemSource property is hooked up to a new set of search results.

    Thanks for your responses,
    -Brad
    Friday, July 4, 2008 9:50 PM
  • Tim Dawson said:

    You can use the below code to capture when any dependency property is changed. I'm not sure if it'll solve your particular issue in this instance, though.

    TypeDescriptor.GetProperties(object)["PropertyName"].AddValueChanged();



    I've used code like this in my hosting controls loaded event:

    TypeDescriptor.GetProperties(this.SearchResults)["ItemsSource"].AddValueChanged(thisnew EventHandler(ListView_ItemsSourceChanged)); 

    And the event handler:

    private void ListView_ItemsSourceChanged(object sender, EventArgs e) 
        // This doesn't get fired 

    However, the event isn't being captured when the ItemsSource is changed (however the ItemsSource is bound - I am not sure if that makes a difference?) I am using this incorrectly?

    Cheers,
    -Brad


    Friday, July 4, 2008 9:55 PM
  •    


    I guess the problem it that you pass this as the first Parameter into method AddValueChanged()

    pls try the following code, that should help you
      TypeDescriptor.GetProperties(listviewcontrol)["ItemsSource"].AddValueChanged(listviewcontrol, new EventHandler(ListView_ItemsSourceChanged));

    I guess 4 your situation, code should be 

    TypeDescriptor.GetProperties(this.SearchResults)["ItemsSource"].AddValueChanged(this.SearchResultsnew EventHandler(ListView_ItemsSourceChanged)); 

    hope that helps


    珍惜生命,远离美女
    • Marked as answer by Brad Leach Sunday, July 6, 2008 11:55 AM
    Saturday, July 5, 2008 10:31 AM
  • AlexTre said:

    I guess the problem it that you pass this as the first Parameter into method AddValueChanged()

    TypeDescriptor.GetProperties(this.SearchResults)["ItemsSource"].AddValueChanged(this.SearchResultsnew EventHandler(ListView_ItemsSourceChanged)); 


    That's perfect - thanks. It's funny how one sometimes misses the obvious errors!

    Cheers,
    -Brad

    Sunday, July 6, 2008 11:57 AM