This is a migrated thread and some comments may be shown as answers.

About SelectionChanged Event

5 Answers 510 Views
GridView
This is a migrated thread and some comments may be shown as answers.
zai yunfeng
Top achievements
Rank 2
zai yunfeng asked on 30 Sep 2009, 07:16 AM

Hi, I've been confused by a problem. First, I set RadGridView.ItemSource with a ObservableCollection and selected a row. Then I removed an item from ObservableCollection, the RadGridView.SelectedItem changed to null and did't trigger SelectionChanged Event. Is that by desgin? Thanks.

Version: Q2 2009.2.813.35

5 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 30 Sep 2009, 09:36 AM
Hello zai yunfeng,

Yes, this is behavior is by design.

If this behavior is causing you trouble feel free to provide us with more information about your scenario and we will try to come up with a solution.

Sincerely yours,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
zai yunfeng
Top achievements
Rank 2
answered on 30 Sep 2009, 11:44 AM
For example, I have a button which context is "Delete". I want to enable button when I select a row and disable button when I select nothing. After I selected a row, ItemSource changed. At this time, "Delete" button should be disabled. I think it's better to do this in SelectionChanged event handler. Because SelectedItem is changed to null.
0
Accepted
Milan
Telerik team
answered on 02 Oct 2009, 12:27 PM
Hello zai yunfeng,

There are two ways to do that. The first one involves the SelectionChanged event:

private void gridView_SelectionChanged(object sender, SelectionChangeEventArgs e)  
{  
    this.UpdateButtonIsEnabledState();  
}  
 
private void UpdateButtonIsEnabledState()  
{  
    if (this.gridView.SelectedItems.Count > 0)  
        this.DeleteButton.IsEnabled = true;  
    else 
        this.DeleteButton.IsEnabled = false;  

When selection changes we check if there are selected items and if there are we enable the button. If there are no selected items the button is disabled. Just remember to call UpdateButtonIsEnabled when RadGridView.ItemsSource is set  so that the button is disabled by default.

The second method involves WPF Commands and basically it is a more fancy way to achieve the same functionality. If you are not familiar with commands you can read more about this topic here .

<Window x:Class="DeleteRowCommand.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    Title="Window1" Height="700" Width="600">  
    <Window.CommandBindings> 
        <CommandBinding Command="ApplicationCommands.Delete"   
                        CanExecute="CommandBinding_CanExecute"   
                        Executed="CommandBinding_Executed"/>  
    </Window.CommandBindings> 
    <Grid> 
        <telerik:RadGridView Name="playersGrid" Grid.Row="1" AutoGenerateColumns="True" 
                ColumnsWidthMode="Auto">  
        </telerik:RadGridView> 
        <Button x:Name="DeleteButton"   
                Content="Delete"   
                HorizontalAlignment="Center" VerticalAlignment="Top"   
                Command="ApplicationCommands.Delete" />     
    </Grid> 
</Window> 

With this XAML code we tell the Button to execute the Delete command when the button is clicked. Unfortunately that is not enough to actually execute the delete logic. The next thing that should be done is to associate the Delete command with an event handler that will contain the actual executable code.
This association is created in Window.CommandBindings.

The CanExecute event handler will determine when the Delete comamnd can be executed (when the delete button is enabled) and the Executed event handler contains the actual implementation of the command. Here is the code:

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)  
{  
    e.CanExecute = this.playersGrid.SelectedItems.Count > 0;  
}  
 
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)  
{  
    // delete item  
    this.data.Remove((Player)this.playersGrid.SelectedItem);  

Hope this information is helpful.
Since the second approach is a bit lengthy I have attached a samle project that demonstrates it.

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
zai yunfeng
Top achievements
Rank 2
answered on 02 Oct 2009, 03:41 PM
Hello Milan,
  I've used the first method to do my project. Thank you so much for all of these.
0
zai yunfeng
Top achievements
Rank 2
answered on 02 Oct 2009, 03:43 PM
Hello Milan,
  I've used the first method to do my project. Thank you so much for all of these.
Tags
GridView
Asked by
zai yunfeng
Top achievements
Rank 2
Answers by
Milan
Telerik team
zai yunfeng
Top achievements
Rank 2
Share this question
or