DateTimeUpDown ContextMenu

Jul 30, 2011 at 10:35 PM

How do I setup a custom Context Menu for a DateTimeUpDown? I keep getting a "Cut, Copy, Paste" context menu,

 

        
            
                
                    
                        
                        
                        
                        
                    
....

                
                
        
Jul 31, 2011 at 1:18 AM

Here is what I have but it doesn't seem to work...

 

            <extToolkit:DateTimeUpDown.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="8:00 AM">
                        <MenuItem Header="8:00 AM" Tag="800" Click="Time_Click"/>
                        <MenuItem Header="8:15 AM" Tag="815" Click="Time_Click"/>
                        <MenuItem Header="8:30 AM" Tag="830" Click="Time_Click"/>
                        <MenuItem Header="8:45 AM" Tag="845" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="9:00 AM">
                        <MenuItem Header="9:00 AM" Tag="900" Click="Time_Click"/>
                        <MenuItem Header="9:15 AM" Tag="915" Click="Time_Click"/>
                        <MenuItem Header="9:30 AM" Tag="930" Click="Time_Click"/>
                        <MenuItem Header="9:45 AM" Tag="945" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="10:00 AM">
                        <MenuItem Header="10:00 AM" Tag="1000" Click="Time_Click"/>
                        <MenuItem Header="10:15 AM" Tag="1015" Click="Time_Click"/>
                        <MenuItem Header="10:30 AM" Tag="1030" Click="Time_Click"/>
                        <MenuItem Header="10:45 AM" Tag="1045" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="11:00 AM">
                        <MenuItem Header="11:00 AM" Tag="1100" Click="Time_Click"/>
                        <MenuItem Header="11:15 AM" Tag="1115" Click="Time_Click"/>
                        <MenuItem Header="11:30 AM" Tag="1130" Click="Time_Click"/>
                        <MenuItem Header="11:45 AM" Tag="1145" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="Noon">
                        <MenuItem Header="Noon" Tag="1200" Click="Time_Click"/>
                        <MenuItem Header="12:15 PM" Tag="1215" Click="Time_Click"/>
                        <MenuItem Header="12:30 PM" Tag="1230" Click="Time_Click"/>
                        <MenuItem Header="12:45 PM" Tag="1245" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="1:00 PM">
                        <MenuItem Header="1:00 PM" Tag="1300" Click="Time_Click"/>
                        <MenuItem Header="1:15 PM" Tag="1315" Click="Time_Click"/>
                        <MenuItem Header="1:30 PM" Tag="1330" Click="Time_Click"/>
                        <MenuItem Header="1:45 PM" Tag="1345" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="2:00 PM">
                        <MenuItem Header="2:00 PM" Tag="1400" Click="Time_Click"/>
                        <MenuItem Header="2:15 PM" Tag="1415" Click="Time_Click"/>
                        <MenuItem Header="2:30 PM" Tag="1430" Click="Time_Click"/>
                        <MenuItem Header="2:45 PM" Tag="1445" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="3:00 PM">
                        <MenuItem Header="3:00 PM" Tag="1500" Click="Time_Click"/>
                        <MenuItem Header="3:15 PM" Tag="1515" Click="Time_Click"/>
                        <MenuItem Header="3:30 PM" Tag="1530" Click="Time_Click"/>
                        <MenuItem Header="3:45 PM" Tag="1545" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="4:00 PM">
                        <MenuItem Header="4:00 PM" Tag="1600" Click="Time_Click"/>
                        <MenuItem Header="4:15 PM" Tag="1615" Click="Time_Click"/>
                        <MenuItem Header="4:30 PM" Tag="1630" Click="Time_Click"/>
                        <MenuItem Header="4:45 PM" Tag="1645" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="5:00 PM">
                        <MenuItem Header="5:00 PM" Tag="1700" Click="Time_Click"/>
                        <MenuItem Header="5:15 PM" Tag="1715" Click="Time_Click"/>
                        <MenuItem Header="5:30 PM" Tag="1730" Click="Time_Click"/>
                        <MenuItem Header="5:45 PM" Tag="1745" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="6:00 PM">
                        <MenuItem Header="6:00 PM" Tag="1800" Click="Time_Click"/>
                        <MenuItem Header="6:15 PM" Tag="1815" Click="Time_Click"/>
                        <MenuItem Header="6:30 PM" Tag="1830" Click="Time_Click"/>
                        <MenuItem Header="6:45 PM" Tag="1845" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="7:00 PM">
                        <MenuItem Header="7:00 PM" Tag="1900" Click="Time_Click"/>
                        <MenuItem Header="7:15 PM" Tag="1915" Click="Time_Click"/>
                        <MenuItem Header="7:30 PM" Tag="1930" Click="Time_Click"/>
                        <MenuItem Header="7:45 PM" Tag="1945" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="8:00 PM">
                        <MenuItem Header="8:00 PM" Tag="2000" Click="Time_Click"/>
                        <MenuItem Header="8:15 PM" Tag="2015" Click="Time_Click"/>
                        <MenuItem Header="8:30 PM" Tag="2030" Click="Time_Click"/>
                        <MenuItem Header="8:45 PM" Tag="2045" Click="Time_Click"/>
                    </MenuItem>
                    <MenuItem Header="9:00 PM">
                        <MenuItem Header="9:00 PM" Tag="2100" Click="Time_Click"/>
                        <MenuItem Header="9:15 PM" Tag="2115" Click="Time_Click"/>
                        <MenuItem Header="9:30 PM" Tag="2130" Click="Time_Click"/>
                        <MenuItem Header="9:45 PM" Tag="2145" Click="Time_Click"/>
                    </MenuItem>

                </ContextMenu>
                </extToolkit:DateTimeUpDown.ContextMenu>
        </extToolkit:DateTimeUpDown>

Jul 31, 2011 at 2:25 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 31, 2011 at 2:27 AM

I never considered the context menu.  I will get this implemented.  By the way, you should really learn about attached events.  Instead of creating an event handler for every single menu item simply define it once on the context menu.

<ContextMenu MenuItem.Click="ContextMenu_Click">
     .....
</ContextMenu>
Jul 31, 2011 at 2:35 AM

Okay, done.  Simply download the latest source code and compile the solution in the Main folder.  Your context menus will work.

Jul 31, 2011 at 12:57 PM

Thanks for the fix for the ContextMenu.  That seems to now work.

About your tip, I'm always willing to learn but when I did this, my event "sender" isn't a MenuItem (i.e. "menuitem" is null in the code below) and fails...

        private void Time_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuitem = (sender as MenuItem);
            int time = int.Parse(menuitem.Tag.ToString());
            int hours = time/100;
            int minutes = time % 100;
            StartTimePicker.Value = new DateTime(1899, 12, 30, hours, minutes, 0);
        }

Seems the calling control is the entire ContextMenu. What am I doing wrong?
Paul P.
Jul 31, 2011 at 3:19 PM

use the e.OriginalSource

Aug 1, 2011 at 12:57 AM

Thanks for the lesson.