在datepicker中仅显示月份 [英] Display only month in datepicker

查看:86
本文介绍了在datepicker中仅显示月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



在我的表格中我有1个日期选择器和3个按钮



1)明智的日期

2)月份明智

3)年度明智



i必须显示日期中的值选择器取决于按钮点击。

因此,如果用户点击月份,那么datepicker应该只显示月份name.same。

请告诉我如何执行此操作

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

推荐答案

您好,

创建表单添加Dtp

在表单加载上添加以下代码事件



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

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







谢谢




Thanks


使用此来自StackOverflow的答案 [ ^ ] with一些数据触发器应该完成这项工作。



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>


这篇关于在datepicker中仅显示月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆