[英]Special-case display value for DateTimeUpDown control
I have a DateTimeUpDown
(from the Extended WPF Toolkit ).我有一个
DateTimeUpDown
(来自扩展的 WPF 工具包)。 I'm using the format string mm:ss
as I'm getting user-input for a time length under an hour.我正在使用格式字符串
mm:ss
,因为我得到用户输入的时间长度不到一小时。 I'd like 00:00
to be a special case, that would mean infinity.我希望
00:00
是一个特例,这意味着无穷大。 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
.从控件中读取 0 对我来说很好,因为我已经在处理此案,但我希望文本变为
Infinity
而不是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
.您可以使用隐藏
WatermarkTextBox
并在Value.TimeOfDay
等于TimeSpan.Zero
时显示TextBlock
的DataTrigger
创建自定义ControlTemplate
。
Here is a complete working example in pure XAML for you:这是纯 XAML 中的完整工作示例:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.