Answered by:
'ItemsControl_SelectionChanged' does not exist in the current context

Question
-
I am
working through the first example How To Enable Multiple Row Selection In LightSwitch's
Grids but I get an error with the last line. "_itemsControl.SelectionChanged += newSelectionChangedEventHandler(ItemsControl_SelectionChanged);"Error 1 The name 'ItemsControl_SelectionChanged' does not exist in the current context C:\Users\Jordan Walker\Documents\Visual Studio 2012\Projects\AddUrlToNavigationMenu\AddUrlToNavigationMenu\Client\UserCode\DemoItemList.cs 48 80 Client
I tried looking up the namespace because it specifically had me adding system.windows.controls to the project even though it already existed in the project by default. I added the using statements above that one because they were generated when I added a code tab for DemoItemList_InitializeDataWorkspace. Other errors were present without those using statements, otherwise my code is the same as the example. I also tried using a prompt visual studio gave me to generate a code stub for the method but that messed things up even more.
My full code is…
using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
//we need this to be able to use DataGrid in our code
using System.Windows.Controls;
namespace LightSwitchApplication
{
public class DemoItemList
{
//although not strictly necessary for this small example
//it's good practice to use constants/variables like these
//in case you want to use them in more than just the ControlAvailable method
//this has to match the actual name of the grid control (by default it gets called "grid")
private const string ITEMS_CONTROL = "grid";
//this is somewhere to store a reference to the grid control
private DataGrid _itemsControl = null;
#region Event Handlers
private void DemoItemList_InitializeDataWorkspace(System.Collections.Generic.List<Microsoft.LightSwitch.IDataService> saveChangesTo)
{
//here we're adding an event handler to get a reference to the grid control, when it becomes available
//and we have no way of knowing when that will be
this.FindControl(ITEMS_CONTROL).ControlAvailable += DemoItems_ControlAvailable;
}
private void DemoItems_ControlAvailable(object send, ControlAvailableEventArgs e)
{
//we know that the control is a grid, but we use TryCast, just in case
_itemsControl = e.Control as DataGrid;
//if the cast failed, just leave, there's nothing more we can do here
if (_itemsControl == null)
{
return;
}
//set the property on the grid that allows multiple selection
_itemsControl.SelectionMode = DataGridSelectionMode.Extended;
_itemsControl.SelectionChanged += new SelectionChangedEventHandler(ItemsControl_SelectionChanged);
}
#endregion
}
}
Monday, September 30, 2013 7:36 PM
Answers
-
It's not a namespace issue. You just forgot to convert some of the sample code:
'this is an optional advanced technique, discussed in the bool that Tim Leung & I are currently writing 'Pro Visual Studio LightSwitch 2011 Development Private Sub ItemsControl_SelectionChanged() _ Handles _itemsControl.SelectionChanged Select Case (_itemsControl Is Nothing) Case True _selectedCount = 0 Case False _selectedCount = _itemsControl.SelectedItems.Count End Select End Sub
found in DemoItemList.vb.
Please ask further questions about the sample in the code gallery sample's "Q and A" tab instead of here.
Justin Anderson, LightSwitch Development Team
- Proposed as answer by Yann DuranModerator Sunday, October 6, 2013 3:09 AM
- Marked as answer by Justin AndersonMicrosoft employee, Moderator Sunday, October 20, 2013 8:22 AM
Tuesday, October 1, 2013 8:25 AMModerator
All replies
-
It's not a namespace issue. You just forgot to convert some of the sample code:
'this is an optional advanced technique, discussed in the bool that Tim Leung & I are currently writing 'Pro Visual Studio LightSwitch 2011 Development Private Sub ItemsControl_SelectionChanged() _ Handles _itemsControl.SelectionChanged Select Case (_itemsControl Is Nothing) Case True _selectedCount = 0 Case False _selectedCount = _itemsControl.SelectedItems.Count End Select End Sub
found in DemoItemList.vb.
Please ask further questions about the sample in the code gallery sample's "Q and A" tab instead of here.
Justin Anderson, LightSwitch Development Team
- Proposed as answer by Yann DuranModerator Sunday, October 6, 2013 3:09 AM
- Marked as answer by Justin AndersonMicrosoft employee, Moderator Sunday, October 20, 2013 8:22 AM
Tuesday, October 1, 2013 8:25 AMModerator -
Oh I was at step 4 and using the code from 'listing 1" not converting anything I will try and add this later chunk to see if it changes anything.
I tried adding this code but the same problem exists with "newSelectionChangedEventHandler(ItemsControl_SelectionChanged);"
Error 17 No overload for 'ItemsControl_SelectionChanged' matches delegate 'System.Windows.Controls.SelectionChangedEventHandler' C:\Users\Jordan Walker\Documents\Visual Studio 2012\Projects\AddUrlToNavigationMenu\AddUrlToNavigationMenu\Client\UserCode\DemoItemList.cs 48 47 Client
And now more errors occur...
Error 16 Missing partial modifier on declaration of type 'LightSwitchApplication.DemoItemList'; another partial declaration of this type exists C:\Users\Jordan Walker\Documents\Visual Studio 2012\Projects\AddUrlToNavigationMenu\AddUrlToNavigationMenu\Client\UserCode\DemoItemList.cs 15 18 Client
Error 18 The name '_selectedCount' does not exist in the current context C:\Users\Jordan Walker\Documents\Visual Studio 2012\Projects\AddUrlToNavigationMenu\AddUrlToNavigationMenu\Client\UserCode\DemoItemList.cs 59 21 Client
Error 19 The name '_selectedCount' does not exist in the current context C:\Users\Jordan Walker\Documents\Visual Studio 2012\Projects\AddUrlToNavigationMenu\AddUrlToNavigationMenu\Client\UserCode\DemoItemList.cs 63 21 Client
My code now looks like this
using System; using System.Linq; using System.IO; using System.IO.IsolatedStorage; using System.Collections.Generic; using Microsoft.LightSwitch; using Microsoft.LightSwitch.Framework.Client; using Microsoft.LightSwitch.Presentation; using Microsoft.LightSwitch.Presentation.Extensions; //we need this to be able to use DataGrid in our code using System.Windows.Controls; namespace LightSwitchApplication { public class DemoItemList { //although not strictly necessary for this small example //it's good practice to use constants/variables like these //in case you want to use them in more than just the ControlAvailable method //this has to match the actual name of the grid control (by default it gets called "grid") private const string ITEMS_CONTROL = "grid"; //this is somewhere to store a reference to the grid control private DataGrid _itemsControl = null; #region Event Handlers private void DemoItemList_InitializeDataWorkspace(List<IDataService> saveChangesTo) { //here we're adding an event handler to get a reference to the grid control, when it becomes available //and we have no way of knowing when that will be this.FindControl(ITEMS_CONTROL).ControlAvailable += DemoItems_ControlAvailable; } private void DemoItems_ControlAvailable(object send, ControlAvailableEventArgs e) { //we know that the control is a grid, but we use TryCast, just in case _itemsControl = e.Control as DataGrid; //if the cast failed, just leave, there's nothing more we can do here if (_itemsControl == null) { return; } //set the property on the grid that allows multiple selection _itemsControl.SelectionMode = DataGridSelectionMode.Extended; _itemsControl.SelectionChanged += new SelectionChangedEventHandler(ItemsControl_SelectionChanged); } //this is an optional advanced technique, discussed in the book //that Tim Leung & I are currently writing //Pro Visual Studio LightSwitch 2011 Development private void ItemsControl_SelectionChanged() { switch (_itemsControl == null) { case true: _selectedCount = 0; break; case false: _selectedCount = _itemsControl.SelectedItems.Count; break; } } #endregion partial void ProcessItems_CanExecute(ref bool result) { // Write your code here. } } }
- Edited by reigh7 Friday, October 4, 2013 9:44 PM
Friday, October 4, 2013 9:09 PM -
For error 17:
ItemsControl_SelectionChanged
needs to be changed to
ItemsControl_SelectionChanged(object sender, SelectionChangedEventArgs args)
When I wrote this, I was using VB exclusively. In VB you're able to omit the parameter declarations, but in C# apparently you can't.
For error 18:
Again in VB you don't need to specify the partial keyword for all files, but in C# you do.
For error 19:
It looks like the local variable declaration for _SelectedCount is missing. If you're not using the "advanced" technique, you can comment those lines out. If you are using it, just add
private _SelectedCount = 0;
underneath
private DataGrid _itemsControl = null;
Yann Duran
- Co-Author of Pro Visual Studio LightSwitch 2011
- Author of the LightSwitch Central Blog
FREE Download: Luminous Tools for LightSwitch
(a Visual Studio productivity extension for LightSwitch)
Click Mark as Answer, if someone's reply answers your question
ClickVote as Helpful, if someone's reply is helpful
By doing this you'll help everyone find answers faster. Sunday, October 6, 2013 3:32 AMModerator -
do you mean to replace all of
"new SelectionChangedEventHandler(ItemsControl_SelectionChanged);"
with
"ItemsControl_SelectionChanged(object sender, SelectionChangedEventArgs args); " Because I still receive errors Not sure where to insert this after trying several ways.
Monday, October 7, 2013 5:47 PM -
No, he wants you to replace:
private void ItemsControl_SelectionChanged()
with:
private void ItemsControl_SelectionChanged(object sender, SelectionChangedEventArgs args)
Just curious, but have you not written event handlers before?
Justin Anderson, LightSwitch Development Team
Monday, October 7, 2013 8:41 PMModerator -
Not in C# only in HTML & ASP.
also what do I have to add to
public class DemoItemList
Error 16 Missing partial modifier on declaration of type 'LightSwitchApplication.DemoItemList'; another partial declaration of this type exists C:\Users\Jordan Walker\Documents\Visual Studio 2012\Projects\AddUrlToNavigationMenu\AddUrlToNavigationMenu\Client\UserCode\DemoItemList.cs 15 18 Client
- Edited by reigh7 Monday, October 7, 2013 8:57 PM
Monday, October 7, 2013 8:46 PM