【问题标题】:Customized Datepicker does not popup calendar自定义日期选择器不弹出日历
【发布时间】:2011-09-25 12:27:18
【问题描述】:

我已经自定义了日期选择器样式,但是由于某种原因,当我单击日期选择器时,日历没有弹出并且没有激活 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>

任何帮助将不胜感激。

【问题讨论】:

    标签: wpf wpf-controls styles datepicker


    【解决方案1】:

    如果您取出命名部分(即名称以“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 不为空,以防止 DatePicker 设置内容。

    一种方法可能是使用 {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>
    

    【讨论】:

    • 谢谢 近 2 周以来,我一直在努力解决这个问题。我按照建议将所有内容更改为 PART_Button,并将 PART_Button Content="{TemplateBinding SelectedDate}" 更改为 Content="{Binding Path=SelectedDate, RelativeSource={RelativeSource TemplatedParent}, TargetNullValue=1/1/2011}"
    • 上面的评论有点复杂(虽然正确)。基本上将 x:Name="Button", x:Name="TextBox", x:Name="Popup" 分别改为 x:Name"PART_Button", PART_TextBox, 和 PART_Popup
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多