我有一个DateTimeUpDown
(来自Extended WPF Toolkit)。我正在使用格式字符串mm:ss
,因为我在一个小时内获得用户输入。我想00:00
成为一个特例,这意味着无穷大。从控件中读取 0 对我来说很好,因为我已经在处理这个案例了,但是我希望文本变成Infinity
而不是00:00
. 我怎么能这样做?
问问题
125 次
1 回答
1
您可以创建一个自定义ControlTemplate
,DataTrigger
其中隐藏了并在等于时WatermarkTextBox
显示 a 。TextBlock
Value.TimeOfDay
TimeSpan.Zero
这是一个完整的纯 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>
于 2019-10-03T14:10:16.100 回答