我有自定义的datepicker样式,但由于某些原因,当我点击日期选择器时,日历不会弹出,也不会激活mouseup子程序。
如果我将Button x:Name =“Button”更改为Button x:Name =“PART_Button”,将弹出日历,但TextBlocks内容将显示:“Show Calendar”
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
xmlns:iPodConv="clr-namespace:Custom_DatePicker.CustomDateConverter"
xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework">
<Style x:Key="CustomDatePickerStyle" TargetType="controls:DatePicker">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Background" Value="#FFFFFFFF"/>
<Setter Property="Padding" Value="2"/>
<!--Setter Property="SelectionBackground" Value="#FF444444"/-->
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint=".5,0" StartPoint=".5,1">
<GradientStop Color="#FF617584" Offset="0"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FFA3AEB9" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:DatePicker">
<Grid x:Name="PART_Root">
<Grid.Resources>
<SolidColorBrush x:Key="DisabledBrush" Color="#8CFFFFFF"/>
<ControlTemplate x:Key="DropDownButtonTemplate" TargetType="Button">
<Grid>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CommonStates">
<vsm:VisualStateGroup.Transitions>
<vsm:VisualTransition GeneratedDuration="0"/>
<vsm:VisualTransition GeneratedDuration="0:0:0.1" To="MouseOver"/>
<vsm:VisualTransition GeneratedDuration="0:0:0.1" To="Pressed"/>
</vsm:VisualStateGroup.Transitions>
<vsm:VisualState x:Name="Normal"/>
<vsm:VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="#FF448DCA"/>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#CCFFFFFF"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#F2FFFFFF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#FF448DCA"/>
</ColorAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="Highlight" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#EAFFFFFF"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#C6FFFFFF"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#6BFFFFFF"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#F4FFFFFF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="0" Duration="00:00:00.001" Storyboard.TargetName="DisabledVisual" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Grid Height="48" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="59" Background="#11FFFFFF">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*"/>
<ColumnDefinition Width="30*"/>
<ColumnDefinition Width="30*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="6*"/>
<RowDefinition Height="20*"/>
<RowDefinition Height="20*"/>
<RowDefinition Height="30*"/>
</Grid.RowDefinitions>
<Border x:Name="Highlight" Margin="-1" Opacity="0" Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="4" BorderBrush="#FF6DBDD1" BorderThickness="1" CornerRadius="0,0,1,1"/>
<Border x:Name="Background" Margin="0,-1,0,0" Opacity="1" Grid.ColumnSpan="4" Grid.Row="1" Grid.RowSpan="3" Background="#FF1F3B53" BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius=".5"/>
<Border x:Name="BackgroundGradient" Margin="0,-1,0,0" Opacity="1" Grid.ColumnSpan="4" Grid.Row="1" Grid.RowSpan="3" BorderBrush="#BF000000" BorderThickness="1" CornerRadius=".5">
<Border.Background>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#F9FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.625"/>
<GradientStop Color="#C6FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Rectangle StrokeThickness="1" Grid.ColumnSpan="4" Grid.RowSpan="2" Margin="0,0,0,2">
<Rectangle.Stroke>
<LinearGradientBrush EndPoint="0.48,-1" StartPoint="0.48,1.25">
<GradientStop Color="#FF494949"/>
<GradientStop Color="#FF9F9F9F" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Stroke>
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.3,-1.1" StartPoint="0.46,1.6">
<GradientStop Color="#FFBD4A40"/>
<GradientStop Color="#FFEAAFAF" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=.Content}"
Text="{Binding Converter={StaticResource DateTimeFormatter}, ConverterParameter=DOW}"
FontSize="11" Margin="0,0,0,3"
Grid.Column="0"
Grid.ColumnSpan="4"
Grid.Row="0"
Grid.RowSpan="2"
/>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
Text="{Binding Converter={StaticResource DateTimeFormatter}, ConverterParameter=MMMM}"
FontSize="10" Margin="0,10,0,3"
Grid.Column="0"
Grid.ColumnSpan="4"
Grid.Row="1"
Grid.RowSpan="2"
/>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
DataContext="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}"
Text="{Binding Converter={StaticResource DateTimeFormatter}, ConverterParameter=dd}"
FontSize="26" Margin="0,0,0,-3"
Grid.Column="0"
Grid.ColumnSpan="4"
Grid.Row="2"
Grid.RowSpan="2" />
<Border x:Name="PART_DisabledVisual" Opacity="0" Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="4" BorderBrush="#B2FFFFFF" BorderThickness="1" CornerRadius="0,0,.5,.5"/>
</Grid>
</Grid>
</ControlTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CommonStates">
<vsm:VisualState x:Name="Normal"/>
<vsm:VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisual" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Button x:Name="Button" Grid.Column="0" Grid.ColumnSpan="4"
Content="{TemplateBinding SelectedDate}"
Template="{StaticResource DropDownButtonTemplate}"
Margin="2,0,2,0"
Width="60"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Foreground="{TemplateBinding Foreground}"/>
<Grid x:Name="DisabledVisual" IsHitTestVisible="False" Opacity="0" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="#8CFFFFFF" RadiusX="1" RadiusY="1"/>
<Rectangle Fill="#8CFFFFFF" RadiusX="1" RadiusY="1" Height="18" Margin="2,0,2,0" Width="24" Grid.Column="1"/>
</Grid>
<Popup x:Name="PART_Popup"
PlacementTarget="{Binding ElementName=Button}"
Placement="Bottom"
StaysOpen="False"
AllowsTransparency="True"
PopupAnimation="Slide"
Focusable="True" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
任何帮助将不胜感激。
答案 0 :(得分:4)
如果你取出命名部分(即名称以“PART_”开头的控件),那么你通常会失去一些功能。在您的情况下,以下代码位于DatePicker的OnApplyTemplate中:
this._dropDownButton = (base.GetTemplateChild("PART_Button") as Button);
if (this._dropDownButton != null)
{
this._dropDownButton.Click += new RoutedEventHandler(this.DropDownButton_Click);
this._dropDownButton.AddHandler(UIElement.MouseLeaveEvent, new MouseEventHandler(this.DropDownButton_MouseLeave), true);
if (this._dropDownButton.Content == null)
{
this._dropDownButton.Content = SR.Get("DatePicker_DropDownButtonName");
}
}
您可以看到DatePicker设置内容(如果为null)并注册单击处理程序。后者打开弹出窗口。
现在,当调用OnApplyTemplate时,SelectedDate可能为null,这就是DatePicker设置按钮内容的原因。您需要确保SelectedDate不为null,以防止DatePicker设置Content。
一种方法可能是使用{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedDate, TargetNullValue="1/1/2011"}
之类的东西,但您可能需要使用长语法:
xmlns:system="clr-namespace:System;assembly=mscorlib"
<!-- ... -->
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedDate">
<Binding.TargetNullValue>
<system:DateTime>1/1/2011</system:DateTime>
</Binding.TargetNullValue>
</Binding>