【发布时间】:2016-07-26 16:21:54
【问题描述】:
在使用 DatePicker 时遇到了一些问题。有点卡住了,可以使用一些帮助/重新审视问题。
目标是为 DatePicker 创建自定义样式,主要区别在于:
问题:
我正在使用来自资源字典的自定义样式和键。
到目前为止,我通过了第一个目标,并且发现第二个目标没有问题。但最后一个,男孩,似乎很艰难。唯一可行的解决方案是设置属性 Focusable="False",但这不是一个选项,因为它会切断手动输入日期的能力。
风格如下:
<Style x:Key="DatePicker" TargetType="{x:Type DatePicker}">
<Setter Property="Foreground" Value="{StaticResource DarkGrayBrush}" />
<Setter Property="Focusable" Value="True" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="IsTodayHighlighted" Value="True" />
<Setter Property="SelectedDateFormat" Value="Short" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<!--Set CalendarStyle to DatePickerCalendarStyle.-->
<Setter Property="CalendarStyle"
Value="{DynamicResource DatePickerCalendarStyle}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DatePicker}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
CornerRadius="0"
Background="Transparent">
<Grid x:Name="PART_Root"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button x:Name="PART_Button"
Grid.Column="1"
Foreground="{TemplateBinding Foreground}"
Focusable="True"
HorizontalAlignment="Left"
Margin="1,0,1,0"
Grid.Row="0"
VerticalAlignment="Center" Style="{StaticResource IconButtonStyle}">
<Image Source="/img/calendar.png" Height="20" />
</Button>
<Border BorderBrush="{StaticResource BlueBrush}"
BorderThickness="2"
CornerRadius="0"
Padding="0"
Width="150"
Height="23"
Background="{StaticResource WhiteBrush}">
<DatePickerTextBox x:Name="PART_TextBox"
Grid.Column="0"
Foreground="{TemplateBinding Foreground}"
Focusable="{TemplateBinding Focusable}"
FocusVisualStyle="{x:Null}"
HorizontalContentAlignment="Left"
Grid.Row="0"
SelectionBrush="{x:Null}"
VerticalContentAlignment="Center"
BorderThickness="0"
Background="Transparent"
Width="150">
</DatePickerTextBox>
</Border>
<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>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
任何帮助将不胜感激。
谢谢。
更新:
这是我的solution的链接:
【问题讨论】:
标签: wpf xaml datepicker