简体   繁体   中英

Special-case display value for DateTimeUpDown control

I have a DateTimeUpDown (from the Extended WPF Toolkit ). I'm using the format string mm:ss as I'm getting user-input for a time length under an hour. I'd like 00:00 to be a special case, that would mean infinity. Reading 0 from the control is fine with me, as I'm already handling the case, but I'd like the text to become Infinity instead of 00:00 . How could I do this?

You could create a custom ControlTemplate with a DataTrigger that hides the WatermarkTextBox and displays a TextBlock when Value.TimeOfDay equals TimeSpan.Zero .

Here is a complete working example in pure XAML for you:

<xctk:DateTimeUpDown Value="{Binding Date}" Format="Custom" FormatString="mm:ss">
    <xctk:DateTimeUpDown.Template>
        <ControlTemplate TargetType="{x:Type xctk:DateTimeUpDown}">
            <xctk:ButtonSpinner x:Name="PART_Spinner"
                                    IsTabStop="False"
                                    Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    HorizontalContentAlignment="Stretch"
                                    VerticalContentAlignment="Stretch"
                                    ButtonSpinnerLocation="{TemplateBinding ButtonSpinnerLocation}"
                                    AllowSpin="{TemplateBinding AllowSpin}"
                                    ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}">
                <Grid>
                    <xctk:WatermarkTextBox x:Name="PART_TextBox"
                                          BorderThickness="0"
                                          Background="Transparent"
                                          ContextMenu="{TemplateBinding ContextMenu}"
                                          FontFamily="{TemplateBinding FontFamily}"
                                          FontSize="{TemplateBinding FontSize}"
                                          FontStretch="{TemplateBinding FontStretch}"
                                          FontStyle="{TemplateBinding FontStyle}"
                                          FontWeight="{TemplateBinding FontWeight}"
                                          Foreground="{TemplateBinding Foreground}"
                                          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                          IsUndoEnabled="{Binding IsUndoEnabled, RelativeSource={RelativeSource TemplatedParent}}"
                                          IsTabStop="True"
                                          MinWidth="20"
                                          AcceptsReturn="False"
                                          Padding="{TemplateBinding Padding}"
                                          TextAlignment="{TemplateBinding TextAlignment}"
                                          TextWrapping="NoWrap"
                                          TabIndex="{TemplateBinding TabIndex}"
                                          Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"
                                          Watermark="{TemplateBinding Watermark}"
                                          WatermarkTemplate="{TemplateBinding WatermarkTemplate}" />
                    <TextBlock x:Name="infinityTb" Text="Infinity" FontFamily="{TemplateBinding FontFamily}"
                                          FontSize="{TemplateBinding FontSize}"
                                          FontStretch="{TemplateBinding FontStretch}"
                                          FontStyle="{TemplateBinding FontStyle}"
                                          FontWeight="{TemplateBinding FontWeight}"
                                          Foreground="{TemplateBinding Foreground}"
                                          TextAlignment="{TemplateBinding TextAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Visibility="Collapsed"/>
                </Grid>
            </xctk:ButtonSpinner>
            <ControlTemplate.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter TargetName="PART_TextBox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=PART_TextBox}" />
                </Trigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="False" />
                        <Condition Binding="{Binding AllowTextInput, RelativeSource={RelativeSource Self}}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsReadOnly" Value="True" TargetName="PART_TextBox" />
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True">
                    <Setter Property="IsReadOnly" Value="True" TargetName="PART_TextBox" />
                </DataTrigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                </Trigger>
                <DataTrigger xmlns:sys="clr-namespace:System;assembly=mscorlib" 
                                    Binding="{Binding Value.TimeOfDay, RelativeSource={RelativeSource Self}}"
                                     Value="{x:Static sys:TimeSpan.Zero}">
                    <Setter Property="Visibility" Value="Collapsed" TargetName="PART_TextBox" />
                    <Setter Property="Visibility" Value="Visible" TargetName="infinityTb" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </xctk:DateTimeUpDown.Template>
</xctk:DateTimeUpDown>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM