Skip to main content Skip to footer

Selecting Interval with ContextMenu in Silverlight Scheduler

This is a simple blog on how you can show a custom ContextMenu in C1Scheduler and also select the interval that gets right clicked.

Setting up the Context Menu

Setting up the Context Menu on C1Scheduler is very simple. Just add the C1ContextMenuService with C1Menu containing few MenuItems in xaml.


<c1:C1ContextMenuService.ContextMenu>  
     <c1:C1ContextMenu Name="c1ContextMenu1" AutoClose="False">  
          <c1:C1MenuItem Header="MenuItem1"/>  
          <c1:C1MenuItem Header="MenuItem2"/>  
          <c1:C1MenuItem Header="MenuItem3"/>  
          <c1:C1MenuItem Header="MenuItem4"/>  
     </c1:C1ContextMenu>  
</c1:C1ContextMenuService.ContextMenu>  

Selecting the Right Clicked Interval

To find the interval which is right clicked, capture the MouseRightButtonDown event of C1Scheduler and find the VisualIntervalPresenter at the mouse pointer's location. Once you retrieve the VisualInterval, select the corresponding Interval using its 'IsSelected' property.


private void c1Scheduler1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)  
{  
        Point location = e.GetPosition(this);  
        SelectVisualInterval(location);  
}  

private void SelectVisualInterval(Point point)  
{  
        ClearVisualIntervals();  

        Point location = point;  
        lst.Clear();  
        //Get the right clicked Interval and select it  
        VisualIntervalPresenter el = GetUIElement(c1Scheduler1, location);  

        if (el != null)  
        {  
            el.Interval.IsSelected = true;  
            lst.Add(el);  
        }  
}  

public static VisualIntervalPresenter GetUIElement(C1Scheduler sched, Point location)  
{  
IEnumerable<UIElement> els;  

//Finds all elements in host coordinates and returns the matched VisualIntervalPresenter  
try  
{  
     els = VisualTreeHelper.FindElementsInHostCoordinates(location, sched);  

             foreach (UIElement el in els)  
             {  
                if (el is VisualIntervalPresenter)  
                {  
                    return (VisualIntervalPresenter)el;  
                }  
             }  
}  
catch  
{  
}  

return null;  
}  

In the above code block, we have used ClearVisualIntervals() method which clears all selected intervals if C1Scheduler has been grouped.


private void ClearVisualIntervals()  
{  
    for (int i = 0; i < c1Scheduler1.GroupItems.Count; i++)  
    {  
        foreach (var vi in c1Scheduler1.GroupItems[i].VisualIntervals)  
        {  
            vi.IsSelected = false;  
        }  
    }  
}  

Now, when you press the left mouse button on any other Interval, previously selected Interval on right click also remains selected. To avoid this behavior, we'll maintain a list of the selected intervals and clear all intervals in this list in the MouseLeftButtonUp event of C1Scheduler and select the current Visual Interval again.


List<VisualIntervalPresenter> lst = new List<VisualIntervalPresenter>();  

private void c1Scheduler1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
{  
foreach (VisualIntervalPresenter vp in lst)  
    if (vp.Interval != null)  
                    vp.Interval.IsSelected = false;  

lst.Clear();  

if (c1Scheduler1.SelectedVisualInterval != null)  
            c1Scheduler1.SelectedVisualInterval.IsSelected = true;  
}  

And we're done! Download the attached sample for complete implementation Download C# Sample Download VB Sample

MESCIUS inc.

comments powered by Disqus