This question is locked. New answers and comments are not allowed.
Michael MSTG
Top achievements
Rank 1
Michael MSTG
asked on 25 May 2011, 04:13 PM
The problem we are having is once the grid is bound to a paged collection, the SelectedItems count is zero when you select one row at a time using only the checkbox that the GridViewSelectColumn produces.
We are evaluating SelectedItems programmatically to enable a RadMenuItem (to which we have logic to ensure the CanExecute is re-evaluated on menu focus) for the user to perform an additional action. When in debug and testing with a breakpoint, it work perfectly. If you remove the breakpoints, it will not evaluate correctly (the property used always returns false).
One thing to keep in mind here is the bug seems to only happen if you ONLY use the checkbox. If you select each row by simply clicking on the row, it works fine as well. I also removed IsSynchronizedWithCurrentItem (which obviously will auto select the first row when bound) then deselect that row then reselect only via the checkbox and, again, the property returns false.
Grid settings:
We are evaluating SelectedItems programmatically to enable a RadMenuItem (to which we have logic to ensure the CanExecute is re-evaluated on menu focus) for the user to perform an additional action. When in debug and testing with a breakpoint, it work perfectly. If you remove the breakpoints, it will not evaluate correctly (the property used always returns false).
One thing to keep in mind here is the bug seems to only happen if you ONLY use the checkbox. If you select each row by simply clicking on the row, it works fine as well. I also removed IsSynchronizedWithCurrentItem (which obviously will auto select the first row when bound) then deselect that row then reselect only via the checkbox and, again, the property returns false.
Grid settings:
<telerik:RadGridViewAutoGenerateColumns="False"
Height="auto"
IsReadOnly="True"
SelectionMode="Multiple"
ShowGroupPanel="False"
RowIndicatorVisibility="Collapsed"
IsSynchronizedWithCurrentItem="False"
FrozenColumnCount="5">
<telerik:RadGridView.Columns>
<telerik:GridViewSelectColumn />
<telerik:GridViewDataColumn />
Property used to evaluate:
Private ReadOnly Property HasSelectedItem As Boolean
Get
Return myGrid.SelectedItems.Count > 0
End Get
End Property
6 Answers, 1 is accepted
0
Hi Michael MSTG,
Greetings,
Milan
the Telerik team
I was unable to reproduce the issue that you have described. The count of the selected items is correct.
I am sending you my test project. Could you please try to reproduce the issue on this project?
Greetings,
Milan
the Telerik team
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Public Issue Tracking
system and vote to affect the priority of the items
0
Michael MSTG
Top achievements
Rank 1
answered on 26 May 2011, 10:24 PM
turns out the issue was on my code...but not what I expected. I dawned
on me that I wasn't re-evaluating my command with the correct event.
I did want to share back with you however something more closely related to what we have in our environment using your example. The idea is to enable/disable the menu object (in this case a button) based on if the user actually selected something.
XAML:
Code Behind:
ViewModel:
I did want to share back with you however something more closely related to what we have in our environment using your example. The idea is to enable/disable the menu object (in this case a button) based on if the user actually selected something.
XAML:
<UserControl x:Class="RadGridView_SL4_AR_18.MainPage" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:my="clr-namespace:RadGridView_SL4_AR_18" mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="700"> <UserControl.Resources> <my:MyViewModel x:Key="MyViewModel" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource MyViewModel}"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <telerik:RadGridView Name="playersGrid" ItemsSource="{Binding Players}" IsReadOnly="True" SelectionMode="Multiple" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" IsSynchronizedWithCurrentItem="False" FrozenColumnCount="2" AutoGenerateColumns="False"> <telerik:RadGridView.Columns> <telerik:GridViewSelectColumn /> <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" /> <telerik:GridViewDataColumn DataMemberBinding="{Binding Number}" /> <telerik:GridViewDataColumn DataMemberBinding="{Binding Position}" /> <telerik:GridViewDataColumn DataMemberBinding="{Binding Country}" /> </telerik:RadGridView.Columns> </telerik:RadGridView> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Name="Button1" Content="Do Something 1" IsEnabled="{Binding CanShowSomething1}" Click="Button1_Click" Margin="5" HorizontalAlignment="Left" /> <Button Name="Button2" Content="Do Something 2" MouseEnter="Button2_MouseEnter" Margin="5" HorizontalAlignment="Left" /> </StackPanel> </Grid></UserControl>Code Behind:
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Telerik.Windows.Controls;using Telerik.Windows.Data;namespace RadGridView_SL4_AR_18{ public partial class MainPage : UserControl { private MyViewModel _myViewModel; public MainPage() { InitializeComponent(); _myViewModel = (MyViewModel)LayoutRoot.DataContext; } private void Button1_Click(object sender, RoutedEventArgs e) { MessageBox.Show(this.playersGrid.SelectedItems.Count.ToString()); } private void Button2_Click(object sender, RoutedEventArgs e) { } private void Button2_MouseEnter(object sender, MouseEventArgs e) { _myViewModel.ShowSomething1Command.Execute(playersGrid.SelectedItems); } }}ViewModel:
using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.ComponentModel;using System.Collections.ObjectModel;using System.Collections;namespace RadGridView_SL4_AR_18{ public class MyViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private ObservableCollection<Club> clubs; private ObservableCollection<Player> players; private object selectedItem; private Action<ICollection> _myNewFunction; private bool _canShowSomething1; public MyViewModel() { this.CanShowSomething1 = false; _myNewFunction = new Action<ICollection>(MyNewFunction_Execute); this.ShowSomething1Command = new ShowSomething1Command(_myNewFunction); } public ObservableCollection<Club> Clubs { get { if (this.clubs == null) { this.clubs = Club.GetClubs(); } return this.clubs; } } public ObservableCollection<Player> Players { get { if (this.players == null) { this.players = Player.GetPlayers(); } return this.players; } } public object SelectedItem { get { return this.selectedItem; } set { if (value != this.selectedItem) { this.selectedItem = value; this.OnPropertyChanged("SelectedItem"); } } } protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, args); } } private void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } /// <summary> /// /// </summary> public ShowSomething1Command ShowSomething1Command { get; set; } /// <summary> /// /// </summary> public bool CanShowSomething1 { get { return _canShowSomething1; } set { _canShowSomething1 = value; OnPropertyChanged("CanShowSomething1"); } } /// <summary> /// /// </summary> /// <param name="myCollection"></param> private void MyNewFunction_Execute(ICollection myCollection) { if (myCollection.Count > 0) this.CanShowSomething1 = true; else this.CanShowSomething1 = false; } } public class ShowSomething1Command : ICommand { private Action<ICollection> _myFunction; public ShowSomething1Command(Action<ICollection> myNewFunction) { _myFunction = myNewFunction; } public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _myFunction.Invoke(parameter as ICollection); } }}0
Hello Michael MSTG,
Regards,
Milan
the Telerik team
So you would like to have a disabled button when no rows are selected and an enabled one no items are selected? Is that correct?
Regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans?
Do you want to know when a feature you care about is added or when a bug fixed?
Explore the
Telerik Public Issue Tracking
system and vote to affect the priority of the items
0
Kiti
Top achievements
Rank 1
answered on 22 Jul 2011, 01:32 PM
Hi,
I also wanted the same feature using GridViewSelectColumn, i.e, when any row is selected the button will be enable and when not, the button will be disabled.
Can you please help me?
I also wanted the same feature using GridViewSelectColumn, i.e, when any row is selected the button will be enable and when not, the button will be disabled.
Can you please help me?
0
Hello Kiti,
MainPage.xaml.cs
Hope this helps!
Best wishes,
Vanya Pavlova
the Telerik team
You may try something similar to the following:
MainPage.xaml
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}"> <telerik:RadGridView x:Name="clubsGrid" SelectionMode="Multiple" AutoGenerateColumns="False" SelectionChanged="clubsGrid_SelectionChanged" ItemsSource="{Binding Collection}"> <telerik:RadGridView.Columns> <telerik:GridViewSelectColumn/> <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property1}"/> <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property2}"/> <telerik:GridViewDataColumn Header="P1" DataMemberBinding="{Binding Property3}"/> <telerik:GridViewColumn> <telerik:GridViewColumn.CellTemplate> <DataTemplate> <telerik:RadButton IsEnabled="False" Click="RadButton_Click" Content="View"/> </DataTemplate> </telerik:GridViewColumn.CellTemplate> </telerik:GridViewColumn> </telerik:RadGridView.Columns> </telerik:RadGridView> </Grid>MainPage.xaml.cs
public partial class MainPage : UserControl { public MainPage() { // Required to initialize variables InitializeComponent(); } private void RadButton_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello"); } private void clubsGrid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e) { foreach (var row in this.clubsGrid.SelectedItems) { var row1 = this.clubsGrid.ItemContainerGenerator.ContainerFromItem(row) as GridViewRow; var button = row1.ChildrenOfType<RadButton>().FirstOrDefault(); if (button != null) { button.IsEnabled = true; } } } }Hope this helps!
Best wishes,
Vanya Pavlova
the Telerik team
Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!
0
Kiti
Top achievements
Rank 1
answered on 28 Jul 2011, 01:34 PM
Thanks for you help.