Welcome to the Xceed Community Sign in | Join | Help
Community Search  

selection event

Sort Posts: Previous Next
  •  03-26-2007, 1:18 AM Post no. 7462

    selection event

    I can't help thinking that this is a stupid question, but how can I know that a row (in particular, but whatever) has been selected? There doesn't seem to be an event for this, so.... I need to be informed when the selectin has changed.
  •  03-26-2007, 8:49 AM Post no. 7463 in reply to 7462

    Re: selection event

    SelectedItem and SelectedIndex are Dependency Properties, you can register to receive change notification by using a DependencyPropertyDescriptor.

    Also, even if the SelectedItems property is of type IList, you can cast it as an ObservableCollection<object> if you need to receive change notification for "multiple selections".
    Marc Laroche
    Software Developer
    Xceed Software Inc.


    I don’t suffer from insanity, I enjoy every minute of it. - Unknown
  •  03-26-2007, 10:51 PM Post no. 7464 in reply to 7463

    Re: selection event

    Hi, thanks, that's great.
    I have tried it and I have it working now. :)
  •  06-11-2007, 2:11 PM Post no. 7465 in reply to 7464

    Re: selection event

    Is it possible to see an example of wiring up to this Dependency Property for those who are new to WPF.

    Thanks in advance.
  •  06-11-2007, 2:17 PM Post no. 7466 in reply to 7465

    Re: selection event

    here is an example:

    DependencyPropertyDescriptor SelectedItemChangedDescriptor =
    DependencyPropertyDescriptor.FromProperty(DataGridControl.SelectedItemProperty,
    typeof(DataGridControl));

    SelectedItemChangedDescriptor.AddValueChanged(tradeViewGrid,
    new EventHandler(OnSelectedItemChanged));

    note: my grid is called the tradeViewGrid in the example above.

    Here is the event handler:

    private void OnSelectedItemChanged(object sender, EventArgs e)
    {
    // Do something with this event
    }
  •  05-02-2008, 12:18 PM Post no. 11932 in reply to 7466

    Re: selection event

    I have used the above code, and it works great for a single select datagrid.

    However, I have a case where the datagrid selection mode is set to SelectionMode.Extended.  I changed the code what is shown below, and I do not get any notification when the 2nd, 3rd, etc... item is selected. 

    In other words, I select one item, and the event fires.  If I hold down the shift key to select the next item in the list also, the event does not fire, even though the SelectedItems count is now 2, instead of 1.

     Is this a bug, or is there some other way I can get notified when an additional item is selected?

    ---------------------------------------------------------------------------------------------

    DependencyPropertyDescriptor SelectedItemsChangedDescriptor =
    DependencyPropertyDescriptor.FromProperty(DataGridControl.SelectedItemsProperty,
    typeof(DataGridControl));

    SelectedItemsChangedDescriptor.AddValueChanged(myGrid,
    new EventHandler(OnSelectedItemsChanged));

    private void OnSelectedItemsChanged(object sender, EventArgs e)
    {
    // Do something with this event
    }  

    ------------------------------------------------------------------------------------------------------

     

  •  05-02-2008, 12:22 PM Post no. 11933 in reply to 11932

    Re: selection event

    One more thing.  Please put this feature on your requested feature list for upcoming releases.  While this method may work (for the single select case), it would be much nicer to have a public event, similar to the WPF ListView SelectionChanged event, and the Silverlight DataGrid SelectionChanged event.
  •  06-13-2008, 9:51 PM Post no. 12958 in reply to 11933

    Re: selection event

    Please note that the above code, if not cleaned up appears to introduce a reference-based memory leak to your aplication. The datagrid and parent window / control look to remain referenced in static memory. This is quite nasty especially if you have a lot of memory-intense UI elements on your shiny WPF window!

     Clean-up with the following:

    DependencyPropertyDescriptor SelectedItemChangedDescriptor = DependencyPropertyDescriptor.FromProperty(DataGridControl.SelectedItemProperty, typeof(DataGridControl));

    SelectedItemChangedDescriptor.RemoveValueChanged(dataGridClients, OnSelectedItemChanged);

    I would hope that normally clean-up code would be presented for this stuff when presenting a solution! Not everyone is very aware of the subtleties involved in managed GC, and I expect that a lot of people have pasted this in as code-magic to get selection notifications to work.

    Perhaps a safer, more regular event is called for?

  •  09-02-2008, 12:15 PM Post no. 14780 in reply to 12958

    Re: selection event

    Hallo,

    I've tried to cast the SelectedItems to ObservableCollection<object> and recieved the following error:

    Unable to cast object of type 'Xceed.Utils.Collections.ObservableList`1[System.Object]' to type 'System.Collections.ObjectModel.ObservableCollection`1[System.Object]'.

    Here the code:

    public event NotifyCollectionChangedEventHandler SelectionChanged
    {
      add { ((ObservableCollection<object>)_Grid.SelectedItems).CollectionChanged += value; }
      remove { ((ObservableCollection<object>)_Grid.SelectedItems).CollectionChanged -= value; }
    }

    I've tried really hard vor nearly an hour but I wasn't able to find a Xceed.Utils.Collections.ObservableList<T> class neither in any xceed dll I use, nor in the internet.

    Greetings Peter

  •  09-03-2008, 1:10 AM Post no. 14814 in reply to 14780

    Re: selection event

    try this peter:    

    if all you want is notification when it has changed.

    ....in constructor

       INotifyCollectionChanged selectedItems = datagrid.SelectedItems as INotifyCollectionChanged;
       selectedItems.CollectionChanged += new NotifyCollectionChangedEventHandler(selectedItems_CollectionChanged);

    }

    event handler :

    void selectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e){

    //handle me baby

    }

     

    casting something as an observable collection isn't possible because it is a generic collection. all you want is the collection changed method so cast it as an interface and all should be sweet.

     

  •  09-03-2008, 5:04 AM Post no. 14824 in reply to 14814

    Re: selection event

    ok, works fine, thx

    unfortunately the olditems and newitems arrays in the event arguments are null but I think I can handle this.

    Thank you very much

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.