DateTimeUpDown 控件的特殊情况显示值
Special-case display value for DateTimeUpDown control
我有一个DateTimeUpDown
(来自Extended WPF Toolkit)。我正在使用格式字符串 mm:ss
,因为我正在获取一个小时以下的用户输入。我希望 00:00
是一个特例,这意味着无穷大。从控件中读取 0 对我来说很好,因为我已经在处理这种情况,但我希望文本变为 Infinity
而不是 00:00
。我该怎么做?
您可以创建一个带有 DataTrigger
的自定义 ControlTemplate
,当 Value.TimeOfDay
等于 TimeSpan.Zero
时隐藏 WatermarkTextBox
并显示 TextBlock
.
这里有一个完整的纯 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>
我有一个DateTimeUpDown
(来自Extended WPF Toolkit)。我正在使用格式字符串 mm:ss
,因为我正在获取一个小时以下的用户输入。我希望 00:00
是一个特例,这意味着无穷大。从控件中读取 0 对我来说很好,因为我已经在处理这种情况,但我希望文本变为 Infinity
而不是 00:00
。我该怎么做?
您可以创建一个带有 DataTrigger
的自定义 ControlTemplate
,当 Value.TimeOfDay
等于 TimeSpan.Zero
时隐藏 WatermarkTextBox
并显示 TextBlock
.
这里有一个完整的纯 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>