We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

SelectedCard DataContext

Hello,
I am working with the SFKanban control populated by LinqtoSql query.  Populating the control is not an issue, and everything works as expected; however, attempting to get the values from  SelectedCard in a drag or tapped event results in an object reference null error.  I could not find any documentation or samples on how to get these values, so I apologize ahead of time if this is covered elsewhere. Please let me know how I could go about getting these values.  Below is my code. Thanks in advance for your help!


-------------XAML-------------
 
<!-- Data Template for Card -->
<DataTemplate x:Key="CardTemplate">
       <Grid>
             <TextBlock Text="{Binding FirstName}"/>
             <TextBlock Text="{Binding LastName}"/>
        </Grid>
</DataTemplate>

<!-- Kanban -->
 <my2:SfKanban x:Name="TaskList"
                       ColumnMappingPath="Status"
                       AutoGenerateColumns="False"
                      CardTemplate="{StaticResource CardTemplate}" CardDragEnd="TaskList_CardDragEnd" >
            <my2:KanbanColumn Categories="Not Started" Title="Not Started"></my2:KanbanColumn>
            <my2:KanbanColumn Categories="In Progress" Title="In Progress"></my2:KanbanColumn>
            <my2:KanbanColumn Categories="Pending" Title="Pending"></my2:KanbanColumn>
            <my2:KanbanColumn Categories="Completed" Title="Completed"></my2:KanbanColumn>
</my2:SfKanban>

---------Code Behind----------

// LinqtoSql Query
protected override void OnNavigatedTo(NavigationEventArgs e)
{
     //Get the Active Teacher to apply to query
     int teacher = Convert.ToInt32(localSettings.Values["ActiveTeacher"]);
    
     //Query student list based on Active Teacher
     using (var db = new DatabaseContext())
     {
         var query = from t in db.Students
         where t.TeachersId == teacher
                select new Models.Students() { StudentsId = t.StudentsId, FirstName = t.FirstName, LastName = t.LastName, DOB = t.DOB, Status = t.Status };
         TaskList.ItemsSource = query.ToList();
      }
}


// Drag Event
  private async void TaskList_CardDragEnd(object sender, KanbanDragEndEventArgs e)
{
     //Cast SFKanban Datacontext to Students class      
     Models.Students student = (sender as SfKanban).DataContext as Models.Students;
     e.SelectedCard.DataContext = student;
     var selectedstudent = student.StudentsId.ToString();
  
    //Test for valid returned StudentId
     var dialog = new MessageDialog(e.SelectedCard.DataContext.ToString());
     await dialog.ShowAsync();
}


1 Reply

SJ Sumathi Jayaraj Syncfusion Team January 2, 2017 07:22 AM UTC

Hi Jame,   
   
Thanks for contacting our Syncfusion Support.   
   
By default, DataContext for all the controls is exist only if we set explicitly. Otherwise, its value is null. We can get the underlying data for the selected card, from the “Content” property. Please find the below code sample for your reference.     
   
Code sample [C#]: 
 
// Drag Event 
private async void TaskList_CardDragEnd(object sender, KanbanDragEndEventArgs e) 
{ 
    //Casting selected card's Datacontext to Students class        
    Students student = e.SelectedCard.Content as Students; 
    var selectedstudent = student.StudentsId.ToString(); 
 
    //Returned StudentId for the selected card. 
    var dialog = new MessageDialog(selectedstudent); 
    await dialog.ShowAsync(); 
} 
 
 
Regards, 
Sumathi J 


Loader.
Live Chat Icon For mobile
Up arrow icon