Silverlight:通过模板定制设置日期选择器的格式

这是日前被问到的一个小问题,情况是这样的:Silverlight里面有一个DatePicker控件,它可以让用户选择或者输入日期。

image

这个控件其实很不错,既可选择,又可以输入。

但问题就出在这个输入上面,有时候我们可能不希望用户去输入,而是必须选择。但是,我们在DatePicker控件上面却找不到类似于IsReadOnly这样的属性。

其实,这样的需求,可以通过修改DatePicker控件的模板来实现。下面是一个示例

<UserControl
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    x:Class="SilverlightApplication3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    xmlns:dp="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls">

    <Grid
        x:Name="LayoutRoot"
        Background="White">
        <sdk:DatePicker
            Height="40"
            Width="300">
            <sdk:DatePicker.Template>
                <ControlTemplate>
                    <Border
                        BorderBrush="Black"
                        BorderThickness="1">
                        <Grid
                            x:Name="Root">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition
                                    Width="*"></ColumnDefinition>
                                <ColumnDefinition
                                    Width="Auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <dp:DatePickerTextBox
                                IsReadOnly="True"
                                x:Name="TextBox"
                                Margin="5"></dp:DatePickerTextBox>
                            <Button
                                Content="Pick"
                                Grid.Column="1"
                                x:Name="Button"
                                Margin="5"></Button>
                            <Popup
                                x:Name="Popup"></Popup>
                        </Grid>
                    </Border>

                </ControlTemplate>
            </sdk:DatePicker.Template>
        </sdk:DatePicker>
    </Grid>
</UserControl>

 

image

 

怎么理解上面的定义呢?我们用了一个Grid,里面放了三个控件。这里的关键就在于,包括Grid在内的四个控件的名称都是有规定的,而且控件类型也是规定的。请参考下面的文档就知道了

http://msdn.microsoft.com/en-us/library/cc278067(v=vs.95).aspx

 

The following table lists the named parts for the DatePicker control.

DatePicker Part

Type

Description

Root

Grid

The root of the control.

Button

Button

The button that opens and closes the Calendar.

TextBox

DatePickerTextBox

The text box that allows you to input a date.

Popup

Popup

The popup for the DatePicker control.

 

也就是说,这个控件为了支持定制,内部是定制好了所谓的Part,这都是可以替换的,只要名称和类型一样即可。

 

从这个例子看来,其他很多控件也是可以定制的,只要参考下面的介绍即可

http://msdn.microsoft.com/en-us/library/cc278075(v=VS.95).aspx

 

所以说,理解模板,对于Silvelight或者WPF开发来说,是很重要的,给大家参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值