如何在XAML中跳出后从日历文本框中删除日期时间

时间:2011-06-10 04:48:13

标签: .net wpf xaml datetime c#-4.0

datetime 下面的代码在.Net 3.5中运行良好,但在.Net 4.0中,在标签显示后,日历文本框上会有一段时间。当焦点在日历文本上时盒子,时间不适合。只有在标签显示时间出现后,如附图所示。我希望时间参与其中。

使用的代码如下:

<jkdp:XamDateTimeEditor Grid.Row="1"
                        Grid.Column="1"
                        KeyboardNavigation.TabIndex="1"
                        InvalidValueBehavior="RetainValue"
                        gcb:CommandBehavior.Event="LostFocus"
                        gcb:CommandBehavior.Command="{Binding EndDateChangedCommand}"
                        Style="{DynamicResource XamDateTimeEditor.SimpleStyle}"
                        Text="{Binding EndDate, Mode=TwoWay, NotifyOnSourceUpdated=true}" >

<Style x:Key="XamDateTimeEditor.SimpleStyle"
       TargetType="{x:Type igdEd:XamDateTimeEditor}"
       BasedOn="{StaticResource XamDateTimeEditor.DefaultFromStyle}">
    <Setter Property="Height"
            Value="22" />
    <Setter Property="Width"
            Value="150" />
    <Setter Property="Margin"
            Value="10,0,10,0" />
    <Setter Property="VerticalAlignment"
            Value="Center" />
    <Setter Property="HorizontalAlignment"
            Value="Left" />
    <Setter Property="ContextMenu"
            Value="{StaticResource CutCopyPasteEditorStyle}" />

    <Setter Property="vw:InputBindingBehaviour.EnableErrorToolTip"
            Value="True" />
    <Setter Property="Focusable"
            Value="False"></Setter>
    <Setter Property="KeyboardNavigation.IsTabStop"
            Value="False"></Setter>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid Focusable="False"
                      KeyboardNavigation.IsTabStop="False">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <TextBox Name="PART_TextBox"
                             Grid.Column="0"
                             VerticalAlignment="Center"
                             Focusable="True"
                             KeyboardNavigation.IsTabStop="True"
                             Margin="0,0,2,0"
                             Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}"
                             ContextMenu="{Binding Path=ContextMenu, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
                    <TextBlock Name="PART_TextBlock"
                               Grid.Column="0"
                               Focusable="False"
                               KeyboardNavigation.IsTabStop="False"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Left"
                               Margin="3,0,2,0"
                               Text="{Binding Path=NullText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}"
                               Visibility="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NullConverter}, ConverterParameter=Visibility}" />
                    <ToggleButton Grid.Column="1"
                                  Focusable="False"
                                  KeyboardNavigation.IsTabStop="False"
                                  MinWidth="{Binding Path=ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
                                  IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
                        <Image Source="Calendar.bmp"
                               Focusable="False"
                               KeyboardNavigation.IsTabStop="False"
                               Margin="2" />
                    </ToggleButton>
                </Grid>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NullConverter}}"
                                 Value="True">
                        <Setter TargetName="PART_TextBox"
                                Property="Visibility"
                                Value="Collapsed" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="EditTemplate">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type igdEd:XamDateTimeEditor}">
                <Grid   Focusable="False"
                        KeyboardNavigation.IsTabStop="False">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <igdEd:XamDateTimeEditor x:Name="PART_DateTimeEditor"
                                             BorderThickness="0"
                                             Background="Transparent"
                                             Focusable="False"
                                             KeyboardNavigation.IsTabStop="False"
                                             Grid.ColumnSpan="2"
                                             Theme="{Binding Path=Theme, Mode=OneTime, RelativeSource={RelativeSource TemplatedParent}}"
                                             NullText="{Binding Path=NullText, Mode=OneTime, RelativeSource={RelativeSource TemplatedParent}}"
                                             DataMode="{Binding Path=DataMode, Mode=OneTime, RelativeSource={RelativeSource TemplatedParent}}"
                                             InvalidValueBehavior="{Binding Path=InvalidValueBehavior, Mode=OneTime, RelativeSource={RelativeSource TemplatedParent}}"
                                             IsDropDownOpen="{Binding Path=IsChecked, ElementName=PART_ToggleButton, Mode=TwoWay}"
                                             Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}"
                                             Value="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}"
                                             ValueConstraint="{Binding Path=ValueConstraint, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                                             ContextMenu="{Binding Path=ContextMenu, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" />
                    <vw:CommitTextBox x:Name="PART_FocusSite"
                             Grid.Column="0"
                             Focusable="True"
                             KeyboardNavigation.IsTabStop="True"
                             MaxLength="10"
                             Margin="0,0,2,0"
                             Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource DateTimeConverter}, ConverterParameter=DateFromStyle, RelativeSource={RelativeSource TemplatedParent}}"
                             ContextMenu="{Binding Path=ContextMenu, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" 
                             Style="{StaticResource TextBox.NormalStyle}"/>
                    <ToggleButton x:Name="PART_ToggleButton"
                                  Grid.Column="1"
                                  Focusable="False"
                                  KeyboardNavigation.IsTabStop="False"
                                  MinWidth="{Binding Path=ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
                                  IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
                        <ToggleButton.Content>
                            <Image   Focusable="False"
                                     KeyboardNavigation.IsTabStop="False"
                                     Source="Calendar.bmp"
                                     Margin="2" />
                        </ToggleButton.Content>
                    </ToggleButton>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

我在这台机器上没有DLL来验证我的答案,但根据Infragistics' help,您应该将Mask属性设置为{date}。如果我从here正确理解,它应该看起来像这样:

<jkdp:XamDateTimeEditor 
                    .....
                    Mask="{}{date}"
                    ..... />

如果要对使用此样式的所有控件使用相同的Mask,请将此setter添加到样式中:

<Setter Property="Mask" Value="{}{date}"/>

这将强制编辑器使用应用程序文化定义的短日期掩码。您可以根据MSDN's Custom Date and Time Format Strings

中解释的StringFormat约定来设计自己的掩码