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

Selected Item not focussed when handling KeyPress

2 Answers 34 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard Newson
Top achievements
Rank 2
Richard Newson asked on 09 Nov 2011, 12:58 AM
I have a Silverlight app with a RadGridView in it. I am using MVVM-Lite. I have my GridView bound to a Data Collection. Each object in the RGV holds a "IsRead" state attribute. When the "IsRead" is set to "False" I apply a style which is essentially "Bold"; when it is "True" then it applies a style of "Normal Text and Italic". I have a RadContextMenu where the user can right click and select "Is Read". Also, I have implemented an EventTrigger to catch the "KeyUp" event and then bound it to a RelayCommand, passing the Key as a parameter. In my CommandMapping code I assess if the Key was the "Insert Key" then set the "IsRead" accordingly. All these functions work nicely and correctly
.
The challenge I am facing is that after the user presses the "Insert Key" and the Command does its bit, the selected item in the GridView does not appear to still have the focus - i.e. if the user presses say the "Down Key" the selection does not change. Ideally, the user would press "Insert", "Down", "Insert", "Down" x a couple, etc to move around the grid to mark various ones as "IsRead".

How do I keep the focus on the selected item in the grid after the "Insert" is handled?

Segements of the code is as follows:
MainPage.xaml
<telerik:RadGridView
        Grid.Row="0"
        Name="dgCriticalEvents"
        AutoGenerateColumns="False"
        ItemsSource="{Binding CriticalEvents, Mode=TwoWay}"
        SelectedItem="{Binding CurrentCriticalEvent, Mode=TwoWay}"
        IsReadOnly="True"
        RowDetailsVisibilityMode="Collapsed"
        RowDetailsTemplate="{StaticResource CriticalEventDetailsTemplate}"
        AreRowDetailsFrozen="True"
        DataLoadMode="Asynchronous"
        RowIndicatorVisibility="Collapsed"
        CanUserInsertRows="False"
        SelectionMode="Extended"
     
        Background="Transparent"
        Foreground="CornflowerBlue"
 
        HorizontalGridLinesBrush="CornflowerBlue"
        VerticalGridLinesBrush="CornflowerBlue"
 
        RowStyleSelector="{StaticResource CE_RowStyle}"
    >
 
        <!--    This handles the EventToCommand functionality of GalaSoft MVVLite -->
        <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyUp" >
            <cmd:EventToCommand Command="{Binding KeyBoardMarkAsRead, Mode=OneWay}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
 
 
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu Opened="RadContextMenu_Opened" Foreground="#FF004AFF">
            <telerik:RadContextMenu.Items>
                <telerik:RadMenuItem Header="Archive" Command="{Binding RemoveCriticalEvent}"/>
                <telerik:RadMenuItem Header="No Action Req." Command="{Binding CENoActionRequired}"/>
                <telerik:RadMenuItem Header="Mark for Follow Up" Command="{Binding CEFollowUp}" />
                <telerik:RadMenuItem Header="Client Advised" Command="{Binding CEClientAdvised}"/>
                <telerik:RadMenuItem Header="Support Contacted" Command="{Binding CESupportContacted}"/>
                <telerik:RadMenuItem Header="Mark Read/Unread (ins)" Command="{Binding MarkAsRead}" />
                <telerik:RadMenuItem Header="Scheduled Maintenance" Command="{Binding CEScheduledMaintenance}" />
            </telerik:RadContextMenu.Items>
        </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>

MainViewModel.cs

        public RelayCommand<KeyEventArgs> KeyBoardMarkAsRead
        {
            get;
            private set;
        }
 
...
 
public MainViewModel()
        {
             ...
             KeyBoardMarkAsRead = new RelayCommand<KeyEventArgs>(e => { if (e.Key == Key.Insert) { SetSelectedCriticalEventAsRead(); } });
 
             ...
        }
 
 
        //----------------------------------------------------------------------------------------
        private void SetSelectedCriticalEventAsRead()
        {
            foreach (xtn_CriticalEvent ce in SelectedCriticalEventsCollection)
            {
                ce.HaveRead = ce.HaveRead ? false : true;
                SaveSelectedCriticalEvent(ce);
            }
        }
        //----------------------------------------------------------------------------------------
        private void SaveSelectedCriticalEvent(xtn_CriticalEvent Source)
        {
            if (Source != null)
            {
                if (Source.EntityAspect.IsChanged)
                {
                    Repository.SaveCriticalEvent(Source, () =>
                    {
                        //  ToDo:   Do something???
                    });
                };
            };
        }
....


Cheers
Rich N

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 11 Nov 2011, 04:10 PM
Hi Richard Newson,

 When the "Insert" Key is pressed, the GridView handles it. Did you cancel the GridViewCommand before handling it in your own way? As I understand you do not start an insert, you just change a property of an item. If you do not change the current cell and the focus stays at the GridView, then the navigation should works as expected.

I have tested it like so:
1. I select a row
2. I have a command keyboard provider where I delete the commands for the "Insert" Key.

public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
       {
           List<ICommand> commands = base.ProvideCommandsForKey(key).ToList();    
           if (key == Key.Insert)
           {
               return new List<ICommand>();
           }
           else
               return commands;
       }
   }

3. I execute this on KeyUp event:
private void myGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
       {
           if (e.Key == System.Windows.Input.Key.Insert)
           {
               Product product = myGrid.SelectedItem as Product;
               myGrid.Items.EditItem(product);
               product.IsRead = False;
               myGrid.Items.CommitEdit();
           }
       }


4. I press the "Down Key" and the selection moves one position down.

I hope that this information makes your case more clear.  

Best wishes,
Didie
the Telerik team

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

0
Richard Newson
Top achievements
Rank 2
answered on 13 Nov 2011, 10:57 PM
Thanks for the reply.
You are correct that I am not inserting a record - just change a data value and the format of the row.
However, it doesn't appear to matter which key I use - if I change my code to be "if (e.Key == Key.R)" I still have the same problem.
I'm not sure how to cancel the GridViewCommand in this situation, I am using the RelayCommand from MMVMLight toolkit - will look around GalaSoft world for hint's on that.

Tags
GridView
Asked by
Richard Newson
Top achievements
Rank 2
Answers by
Dimitrina
Telerik team
Richard Newson
Top achievements
Rank 2
Share this question
or