Home Forums WPF controls Xceed DataGrid for WPF selection event

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22497 |

    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.

    Imported from legacy forums. Posted by Marie (had 9162 views)

    Xceed Support
    Member
    Post count: 5658

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

    Imported from legacy forums. Posted by Marcus [Xceed] (had 699 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi, thanks, that’s great.
    I have tried it and I have it working now. 🙂

    Imported from legacy forums. Posted by Marie (had 725 views)

    User (Old forums)
    Member
    Post count: 23064

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

    Thanks in advance.

    Imported from legacy forums. Posted by Greg (had 577 views)

    User (Old forums)
    Member
    Post count: 23064

    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
    }

    Imported from legacy forums. Posted by Greg (had 4161 views)

    User (Old forums)
    Member
    Post count: 23064

    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
    }  

    ——————————————————————————————————

     

    Imported from legacy forums. Posted by Todd (had 1238 views)

    User (Old forums)
    Member
    Post count: 23064

    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.

    Imported from legacy forums. Posted by Todd (had 848 views)

    User (Old forums)
    Member
    Post count: 23064

    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?

    Imported from legacy forums. Posted by Nathan (had 922 views)

    User (Old forums)
    Member
    Post count: 23064

    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

    Imported from legacy forums. Posted by Peter (had 655 views)

    User (Old forums)
    Member
    Post count: 23064

    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.

     

    Imported from legacy forums. Posted by Aran (had 1137 views)

    User (Old forums)
    Member
    Post count: 23064

    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

    Imported from legacy forums. Posted by Peter (had 1642 views)

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.