DatePicker override context menu

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 9 years ago by justin stofle
Avatar
I am trying to override the default context menu of your DatePicker control. I tried doing it in xaml (see below), however I only see the default cut, copy, paste commands. This leads me to believe you build your context menu in code. I really dont want to attach to the Loaded event and then override the context menu. Do you have any suggestions? Thanks


<shared:DatePicker Behavior="{Binding Path=Behavior}"
                       Foreground="{TemplateBinding ContentControl.Foreground}"
                       Margin="{TemplateBinding ContentControl.Margin}"
                       VerticalAlignment="Center"
                       Height="{Binding Source={StaticResource Settings}, Path=Default.FieldHeight, Mode=TwoWay}"
                       Value="{Binding Path=Value, Converter={StaticResource DateOnlyConverter}}">
            <shared:DatePicker.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="test1" />
                </ContextMenu>
            </shared:DatePicker.ContextMenu>
        </shared:DatePicker>
[Modified at 06/16/2010 08:02 PM]

Comments (5)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Justin,

I don't believe we do anything with ContextMenu in DateTimePicker. There is a TextBox control used in the DateTimePicker's template. Perhaps that is what is showing the ContextMenu.

What I would recommend is creating a Style for TextBox in your DateTimePicker.Resources and setting the ContextMenu on that with a Setter.


Actipro Software Support

Posted 9 years ago by justin stofle
Avatar
I tried the code below and it did not work either did I make a wrong turn somewhere? I sort of feel that setting the context menu on the datepicker itself should propagate down to the textbox.

<shared:DatePicker Behavior="{Binding Path=Behavior}"
                       Foreground="{TemplateBinding ContentControl.Foreground}"
                       Margin="{TemplateBinding ContentControl.Margin}"
                       VerticalAlignment="Center"
                       Height="{Binding Source={StaticResource Settings}, Path=Default.FieldHeight, Mode=TwoWay}"
                       Value="{Binding Path=Value, Converter={StaticResource DateOnlyConverter}}">
<shared:DatePicker.Resources>
                <ContextMenu x:Key="DateMenu">
                    <MenuItem Header="test" />
                    <MenuItem Command="Cut" />
                    <MenuItem Command="Copy" />
                    <MenuItem Command="Paste" />
                    <Separator />
                    <local:ClearMenu />
                </ContextMenu>
                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                    <Setter Property="ContextMenu" Value="{StaticResource DateMenu}"></Setter>
                </Style>
            </shared:DatePicker.Resources>            
        </shared:DatePicker>
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

I am concerned that the DatePicker control is not one of ours. We have a DateTimePicker control, but no DatePicker control. Would you please double check this? We do use the "shared:" xmlns. Also, our DateTimePicker control doesn't have a Behavior property, which you are using in your above code.

If it is our DateTimePicker control that you are using, please email us a simple sample that shows the issue.

Thanks,
Actipro Support


Actipro Software Support

Posted 9 years ago by justin stofle
Avatar
Here is a simple sample that shows my point. when this program launches you will have the box you can type the date in and a little down arrow to drop the calendar. if you right click in the box you get "cut, copy, paste" if you right click on the arrow you get "test 1 and test 2", i want to be able to right click in the box and see "test 1, test 2"


<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <shared:DateTimePicker IsEditable="True">
            <shared:DateTimePicker.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="test 1"></MenuItem>
                    <MenuItem Header="test 2"></MenuItem>
                </ContextMenu>
            </shared:DateTimePicker.ContextMenu>
        </shared:DateTimePicker>
    </Grid>
</Window>

i tried this (see below) and it too did not work


<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <shared:DateTimePicker IsEditable="True">
            <shared:DateTimePicker.Resources>
                <ContextMenu x:Key="DateMenu">
                    <MenuItem Header="test 1" />
                    <MenuItem Command="test 2" />
                </ContextMenu>
                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                    <Setter Property="ContextMenu" Value="{StaticResource DateMenu}"></Setter>
                </Style>
            </shared:DateTimePicker.Resources>
            <shared:DateTimePicker.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="test 1"></MenuItem>
                    <MenuItem Header="test 2"></MenuItem>
                </ContextMenu>
            </shared:DateTimePicker.ContextMenu>
        </shared:DateTimePicker>
    </Grid>
</Window>

[Modified at 06/18/2010 04:57 PM]
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
We've updated the DateTimePicker for the next maintenance release to have the embedded TextBox bind to the DateTimePicker.ContextMenu property for its ContextMenu. If none is set, it will use its default.


Actipro Software Support

The latest build of this product (v2019.1 build 0681) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.