Unselect doesn't trigger any events, nothing happens

6 posts, 1 answers
  1. Clas Ericson
    Clas Ericson avatar
    31 posts
    Member since:
    Sep 2011

    Posted 14 Nov 2011 Link to this post

    Hello,
    I don't know if this a bug or a feature but I have a gridview where I am allowing the user to select/unselect a row. When the user selects a row, SelectedItem bound to the ViewModel is set properly and the event SelectionChanged is triggered. But when the user clicks to unselect and clicks in the same cell that she clicked to select, the event is not triggered and neither is SelectedItem changed. If the user clicks inside a different cell on the same row everything works perfectly.

    Why do I get this behaviour? Is there anything wrong or just me not understanding the GridView?
    Below is my GridView in XAML.

    Regards, Clas Ericson

    <telerik:RadGridView x:Name="DataGrid" Margin="0,0,5,0"
          GridLinesVisibility="None"
          CanUserFreezeColumns="False"
          BorderBrush="White" BorderThickness="0"
          ShowGroupPanel="False"
          AutoGenerateColumns="False"
          HeaderRowStyle="{StaticResource GridViewHeaderRowStyle1}"
          RowStyle="{StaticResource GridViewRowStyle1}"
          Background="{StaticResource GridViewRowBrush}"
          AlternateRowBackground="{StaticResource GridViewAlternatingRowBrush}"
          AlternationCount="2"                                                         
          Foreground="{StaticResource CountyShapeColor}"
          GroupPanelForeground="{StaticResource CountyShapeColor}"
          IsFilteringAllowed="False"
          CanUserReorderColumns="False"
          CanUserResizeColumns="False"
          GroupPanelBackground="Transparent"
          HorizontalGridLinesBrush="Transparent"
          VerticalGridLinesBrush="White"
          VisibleRowsHeight="0"
          RowHeight="8"
          FontSize="10"
          RowIndicatorVisibility="Collapsed"
          ItemsSource="{Binding SelectList}"
          CanUserSelect="True"
          SelectionMode="Multiple"
          SelectedItem="{Binding SelectedItem,Mode=TwoWay}"
          SelectionChanged="DataGrid_SelectionChanged"
          SelectionUnit="FullRow">
         <telerik:RadGridView.Columns>
             <telerik:GridViewDataColumn IsSortable="False" IsReadOnly="True"
                 Width="*" HeaderCellStyle="{StaticResource myGridHeaderStyleLeft}"
                 CellStyle="{StaticResource GridViewCellStyleLeft}">
                 <telerik:GridViewDataColumn.Header>
                     <Grid Height="39">
                         <Grid.RowDefinitions>
                             <RowDefinition />
                             <RowDefinition />
                         </Grid.RowDefinitions>
                         <Grid.ColumnDefinitions>
                             <ColumnDefinition Width="*"/>
                             <ColumnDefinition Width="*"/>
                             <ColumnDefinition Width="75" />
                         </Grid.ColumnDefinitions>
                         <TextBlock  Margin="10,2,2,2"
                             Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=BuildingInformation}"
                             Grid.ColumnSpan="3"/>
                         <TextBlock x:Name="tbPlaceRealEstate" Margin="10,2,2,2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=RealEstate}"
                            Grid.Row="1"
                            MouseLeftButtonDown="tbGridMouseLeftButtonDown"
                            MouseEnter="TextBlock_MouseEnter"
                            MouseLeave="TextBlock_MouseLeave"/>
                         <TextBlock x:Name="tbPlaceBuilding" Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=Building}" 
                            Grid.Row="1" Grid.Column="1"
                            MouseLeftButtonDown="tbGridMouseLeftButtonDown"
                            MouseEnter="TextBlock_MouseEnter"
                            MouseLeave="TextBlock_MouseLeave"/>
                         <TextBlock x:Name="tbPlaceArea" TextAlignment="Right" Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources}, Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=Area}" 
                            Grid.Row="1" Grid.Column="2"
                            MouseLeftButtonDown="tbGridMouseLeftButtonDown"
                            MouseEnter="TextBlock_MouseEnter"
                            MouseLeave="TextBlock_MouseLeave"/>
                     </Grid>
                 </telerik:GridViewDataColumn.Header>
                 <telerik:GridViewDataColumn.CellTemplate>
                     <DataTemplate>
                         <Grid>
                             <Grid.RowDefinitions>
                                 <RowDefinition />
                             </Grid.RowDefinitions>
                             <Grid.ColumnDefinitions>
                                 <ColumnDefinition Width="*"/>
                                 <ColumnDefinition Width="*"/>
                                 <ColumnDefinition Width="75"/>
                             </Grid.ColumnDefinitions>
                             <TextBlock  Margin="10,2,2,2" Text="{Binding parentObjectName}"
                                 Grid.Column="0" TextAlignment="Left" />
                             <TextBlock Margin="2" Text="{Binding objectName}"
                                 Grid.Column="1" TextAlignment="Left" />
                             <TextBlock Margin="2" Text="{Binding Path=totalArea}"
                                 Grid.Column="2" TextAlignment="Right" />
                         </Grid>
                     </DataTemplate>
                 </telerik:GridViewDataColumn.CellTemplate>
             </telerik:GridViewDataColumn>
             <telerik:GridViewDataColumn IsSortable="False" IsReadOnly="True" Width="150"
                 HeaderCellStyle="{StaticResource myGridHeaderStyle}"
                 CellStyle="{StaticResource GridViewCellStyle}" >
                 <telerik:GridViewDataColumn.Header>
                     <Grid>
                         <Grid.RowDefinitions>
                             <RowDefinition />
                             <RowDefinition />
                         </Grid.RowDefinitions>
                         <Grid.ColumnDefinitions>
                             <ColumnDefinition Width="75"/>
                             <ColumnDefinition Width="75"/>
                         </Grid.ColumnDefinitions>
                         <TextBlock Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=Energy}"
                            Grid.ColumnSpan="2" />
                         <TextBlock x:Name="tbEnergyKWh" Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                Source={StaticResource ApplicationResources},
                                Mode=OneWay,
                                Converter={StaticResource ApplicationResources},
                                ConverterParameter=UnitConsumptionArea}"
                            Grid.Row="1" 
                            MouseLeftButtonDown="tbGridMouseLeftButtonDown"
                            MouseEnter="TextBlock_MouseEnter"
                            MouseLeave="TextBlock_MouseLeave"/>
                         <TextBlock x:Name="tbEnergyDiff" Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=DifferencePercent}"
                            Grid.Row="1" Grid.Column="1" TextAlignment="Right" 
                            MouseLeftButtonDown="tbGridMouseLeftButtonDown"
                            MouseEnter="TextBlock_MouseEnter"
                            MouseLeave="TextBlock_MouseLeave"/>
                     </Grid>
                 </telerik:GridViewDataColumn.Header>
                 <telerik:GridViewDataColumn.CellTemplate>
                     <DataTemplate>
                         <Grid>
                             <Grid.RowDefinitions>
                                 <RowDefinition />
                             </Grid.RowDefinitions>
                             <Grid.ColumnDefinitions>
                                 <ColumnDefinition Width="75" />
                                 <ColumnDefinition Width="75" />
                             </Grid.ColumnDefinitions>
                             <TextBlock Margin="2"
                                 Text="{Binding consumption, StringFormat=\{0:N2\}}"
                                 Grid.Column="0" />
                             <TextBlock Margin="2"
                                 Text="{Binding StringFormat=\{0:P\}, Path=consumptionDifference}"
                                 Grid.Column="1" TextAlignment="Right" />
                         </Grid>
                     </DataTemplate>
                 </telerik:GridViewDataColumn.CellTemplate>
             </telerik:GridViewDataColumn>
             <telerik:GridViewDataColumn IsSortable="False" IsReadOnly="True"
                 Width="150" HeaderCellStyle="{StaticResource myGridHeaderStyle}"
                 CellStyle="{StaticResource GridViewCellStyle}">
                 <telerik:GridViewDataColumn.Header>
                     <Grid>
                         <Grid.RowDefinitions>
                             <RowDefinition />
                             <RowDefinition />
                         </Grid.RowDefinitions>
                         <Grid.ColumnDefinitions>
                             <ColumnDefinition Width="75"/>
                             <ColumnDefinition Width="75"/>
                         </Grid.ColumnDefinitions>
                         <TextBlock Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=Economy}"
                            Grid.ColumnSpan="2" />
                         <TextBlock x:Name="tbEconomyCost" Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=UnitSEKArea}"
                            Grid.Row="1" 
                            MouseLeftButtonDown="tbGridMouseLeftButtonDown"
                            MouseEnter="TextBlock_MouseEnter"
                            MouseLeave="TextBlock_MouseLeave"/>
                         <TextBlock x:Name="tbEconomyDiff" Margin="2"
                            Text="{Binding Path=DynamicTranslatedString,
                                 Source={StaticResource ApplicationResources},
                                 Mode=OneWay,
                                 Converter={StaticResource ApplicationResources},
                                 ConverterParameter=DifferencePercent}"
                            Grid.Row="1" Grid.Column="1"
                            TextAlignment="Right" 
                            MouseLeftButtonDown="tbGridMouseLeftButtonDown"
                            MouseEnter="TextBlock_MouseEnter"
                            MouseLeave="TextBlock_MouseLeave"/>
                     </Grid>
                 </telerik:GridViewDataColumn.Header>
                 <telerik:GridViewDataColumn.CellTemplate>
                     <DataTemplate>
                         <Grid>
                             <Grid.RowDefinitions>
                                 <RowDefinition />
                             </Grid.RowDefinitions>
                             <Grid.ColumnDefinitions>
                                 <ColumnDefinition Width="75"/>
                                 <ColumnDefinition Width="75"/>
                             </Grid.ColumnDefinitions>
                             <TextBlock Margin="2"
                                 Text="{Binding cost, StringFormat=\{0:N2\}}"
                                 Grid.Column="0" />
                             <TextBlock Margin="2"
                                 Text="{Binding StringFormat=\{0:P\}, Path=costDifference}"
                                 Grid.Column="1" TextAlignment="Right" />
                         </Grid>
                     </DataTemplate>
                 </telerik:GridViewDataColumn.CellTemplate>
             </telerik:GridViewDataColumn>
         </telerik:RadGridView.Columns>
     </telerik:RadGridView>


  2. Nick
    Admin
    Nick avatar
    593 posts

    Posted 14 Nov 2011 Link to this post

    Hello Clas,

    I tried to reproduce the problem, but unfortunetely I was not able to. The current item is changing even if the the clicked cell goes to edit mode. I am attaching the project I worked with so you can look into it. I would apreciate it if you could send a runable project demonstrating the problem so we can look into it.

    Kind regards,
    Nikolay Vasilev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Clas Ericson
    Clas Ericson avatar
    31 posts
    Member since:
    Sep 2011

    Posted 15 Nov 2011 Link to this post

    Hello Nikolay,
    thank you for your reply! I have updated your example project with the grid I'm using and also updated the model and the viewmodel to fit the grid. Unfortunately I managed to reproduce the error. You can download the solution from http://db.tt/OIUO253O It is a ZIP-file from my Dropbox account.

    Regards, Clas
  4. Answer
    Nick
    Admin
    Nick avatar
    593 posts

    Posted 15 Nov 2011 Link to this post

    Hi Clas,

     I can suggest the following workaround. 

    private void UnselectButton_Click(object sender, RoutedEventArgs e)
            {
                this.DataGrid.SelectedItems.Clear();
                this.DataGrid.CurrentCellInfo = new GridViewCellInfo(new SampleNode(), this.DataGrid.Columns[0]);
            }
    It works fine with your project. I am attaching it so you can check it out.  Regards,
    Nikolay Vasilev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Clas Ericson
    Clas Ericson avatar
    31 posts
    Member since:
    Sep 2011

    Posted 17 Nov 2011 Link to this post

    Hi Nikolay,
    thank you for your workaround! Unfortunately it's not possible to use a separat unselect button because the application has a strict graphical design and therefor needs to be able to unselect a row by clicking on it.
    I add three mouse related events to each row when gridview.rowloaded fires, row.mouseenter, row.mouseleave and row.mouseleftbuttondown. But only the mouse enter and leave events are triggered, it's like this in my solution and in your updated example.

    Frankly, to me this seems like an oddity very close to a bug.

    Regards, Clas Ericson
  6. Clas Ericson
    Clas Ericson avatar
    31 posts
    Member since:
    Sep 2011

    Posted 17 Nov 2011 Link to this post

    Update
    I managed to, perhaps not solve the issue, but at least fix a workaround that worked in my case. Instead of adding a MouseLeftButtonDown-eventhandler for every row in the RowLoaded event I added an eventhandler in the constructor. See example below.

    Thank you anyway, Nikolay!

    Instead of:

    void radGridView_RowLoaded(object sender,
                      Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
    {
        var row = e.Row as Telerik.Windows.Controls.GridView.GridViewRow;
        if (row != null)
        {
            row.MouseLeftButtonDown +=
                new MouseButtonEventHandler(row_MouseLeftButtonUp);               
        }
    }

    I did EventHandler in constructor:

      public MainPage()
    {
        InitializeComponent();
         // ...
        this.AddHandler(GridViewRow.MouseLeftButtonDownEvent,
             new MouseButtonEventHandler(this.row_MouseLeftButtonDown), true);
    }
Back to Top