我能够在DatePicker的日历控件上添加清除按钮,但是我想知道是否可以在DatePicker文本框中添加相同的按钮吗?
<DatePicker >
<DatePicker.CalendarStyle>
<Style TargetType="Calendar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Calendar}">
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel x:Name="PART_Root" HorizontalAlignment="Center" Background="LightBlue">
<CalendarItem x:Name="PART_CalendarItem"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Style="{TemplateBinding CalendarItemStyle}"
Height="Auto" Width="Auto"/>
<Button Content="Clear Date" Click="Button_Click"
x:Name="PART_ClearButton"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="2"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.CalendarStyle>
</DatePicker>
看看DatePicker Styles and Templates。
修改PART_Root:
<Grid x:Name="PART_Root" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<!--ADD ANOTHER COLUMN HERE-->
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!--CHANGE COLUMN=1 TO COLUMN =2-->
<Button x:Name="PART_Button"
Grid.Column="2"
Foreground="{TemplateBinding Foreground}"
Focusable="False"
HorizontalAlignment="Left"
Margin="3,0,3,0"
Grid.Row="0"
Style="{StaticResource DropDownButtonStyle}"
VerticalAlignment="Top" />
<!--//ADD THE BUTTON HERE-->
<Button Grid.Column="1" Content="X" Foreground="Red" Width="20" Height="20"/>
<DatePickerTextBox x:Name="PART_TextBox"
Grid.Column="0"
Foreground="{TemplateBinding Foreground}"
Focusable="{TemplateBinding Focusable}"
HorizontalContentAlignment="Stretch"
Grid.Row="0"
VerticalContentAlignment="Stretch" />
<Grid x:Name="PART_DisabledVisual"
Grid.ColumnSpan="2"
Grid.Column="0"
IsHitTestVisible="False"
Opacity="0"
Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0"
Fill="#A5FFFFFF"
RadiusY="1"
Grid.Row="0"
RadiusX="1" />
<Rectangle Grid.Column="1"
Fill="#A5FFFFFF"
Height="18"
Margin="3,0,3,0"
RadiusY="1"
Grid.Row="0"
RadiusX="1"
Width="19" />
<Popup x:Name="PART_Popup"
AllowsTransparency="True"
Placement="Bottom"
PlacementTarget="{Binding ElementName=PART_TextBox}"
StaysOpen="False" />
</Grid>
</Grid>
结果将是: