Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

In my form i have 1 date picker and 3 buttons

1)date wise
2)month wise
3)year wise

i have to display the value in the date picker depending on button click.
so if the user clicks month wise then the datepicker should display only month name.same on year wise.
please tell me how to do this
Posted

Hi,
create a form add Dtp on it
Add the following code on the form load event

VB
DateTimePicker1.Format = DateTimePickerFormat.Custom
        DateTimePicker1.CustomFormat = "MMMM"



Thanks
 
Share this answer
 
Comments
[no name] 30-Nov-13 4:35am    
This code will not work for wpf date picker..i had already tried this..
Using this answer from StackOverflow[^] with some datatriggers should do the job.

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DatePicker Height="30" HorizontalAlignment="Left" Margin="73,48,0,0" Name="DatePicker1" VerticalAlignment="Top" Width="299" />
        <RadioButton Content="Year" Height="16" HorizontalAlignment="Left" Margin="90,122,0,0" Name="rbYear"  VerticalAlignment="Top" />
        <RadioButton Content="Month" Height="16" HorizontalAlignment="Left" Margin="88,162,0,0" Name="rbMonth" VerticalAlignment="Top" IsChecked="True" />
        <RadioButton Content="Date" Height="16" HorizontalAlignment="Left" IsChecked="False" Margin="90,195,0,0" Name="rbDate" VerticalAlignment="Top" />
    </Grid>
    <Window.Resources>
        <Style  TargetType="{x:Type DatePickerTextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=rbYear}" Value="True">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='yyyy', 
                           RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" >
                                </TextBox>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsChecked, ElementName=rbMonth}" Value="True">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='MMMM', 
                           RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" >
                                </TextBox>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsChecked, ElementName=rbDate}" Value="True">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd', 
                           RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" >
                                </TextBox>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>                
            </Style.Triggers>
        </Style>
    </Window.Resources>
</Window>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900