DecimalUpDown(扩展的WPF工具包)-仅在丢失焦点时更新源代码
本文关键字:焦点 源代码 更新 扩展 WPF 工具包 DecimalUpDown | 更新日期: 2023-09-27 18:09:41
我正在使用扩展WPF工具包的DecimalUpDown控件,其Value属性绑定到十进制?如下:
<extToolkit:DecimalUpDown Value="{Binding BlahBlah, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ShowButtonSpinner="False" />
private Decimal? blahblah = 5;
public Decimal? BlahBlah
{
get { return blahblah; }
set { blahblah = value; }
}
我注意到,当我在文本框中键入数字时,值不会更新,直到我单击控件外部。它的ValueChanged事件也不会被触发,直到我点击外部。
我打算在用户更改值时立即更新值(即实时)。有办法做到这一点吗?
是的,您必须将控件模板替换为具有UpdateSourceTrigger=PropertyChanged的模板。去年我复制了现有的模板,进行了修改,然后在我的控件中使用它。新资源:
<ControlTemplate x:Key="newDecimalUpDownTemplate"
TargetType="{x:Type Control}">
<extToolkit:ButtonSpinner x:Name="Spinner"
AllowSpin="{Binding AllowSpin, RelativeSource={RelativeSource TemplatedParent}}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
IsTabStop="False"
ShowButtonSpinner="{Binding ShowButtonSpinner, RelativeSource={RelativeSource TemplatedParent}}">
<extToolkit:WatermarkTextBox x:Name="TextBox"
AcceptsReturn="False"
BorderThickness="0"
Background="{TemplateBinding Background}"
ContextMenu="{TemplateBinding ContextMenu}"
Foreground="{TemplateBinding Foreground}"
FontWeight="{TemplateBinding FontWeight}"
FontStyle="{TemplateBinding FontStyle}"
FontStretch="{TemplateBinding FontStretch}"
FontSize="{TemplateBinding FontSize}"
FontFamily="{TemplateBinding FontFamily}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
MinWidth="20"
SelectAllOnGotFocus="{Binding SelectAllOnGotFocus, RelativeSource={RelativeSource TemplatedParent}}"
TextAlignment="{Binding TextAlignment, RelativeSource={RelativeSource TemplatedParent}}"
TextWrapping="NoWrap"
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
TabIndex="{TemplateBinding TabIndex}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
WatermarkTemplate="{Binding WatermarkTemplate, RelativeSource={RelativeSource TemplatedParent}}"
Watermark="{Binding Watermark, RelativeSource={RelativeSource TemplatedParent}}">
<extToolkit:WatermarkTextBox.IsReadOnly>
<Binding Path="IsEditable" RelativeSource="{RelativeSource TemplatedParent}">
<Binding.Converter>
<Converters:InverseBoolConverter/>
</Binding.Converter>
</Binding>
</extToolkit:WatermarkTextBox.IsReadOnly>
</extToolkit:WatermarkTextBox>
</extToolkit:ButtonSpinner>
</ControlTemplate>
In my Control:
Template="{StaticResource newDecimalUpDownTemplate}"
我自己也花了一些时间在这个问题上,并找到了一个相当不错的解决方案,我编辑了模板(右键单击DecimalUpDown,然后去编辑模板编辑模板,谢谢Ben你节省了我一些严肃的时间->如何覆盖一个样式),并将其与辉煌的emorog解决方案相结合!
我写这一切的风格:
<Style x:Key="DecimalUpDownStyle1" TargetType="{x:Type xctk:DecimalUpDown}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type xctk:DecimalUpDown}">
<xctk:ButtonSpinner x:Name="PART_Spinner" AllowSpin="{Binding AllowSpin, RelativeSource={RelativeSource TemplatedParent}}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ButtonSpinnerLocation="{Binding ButtonSpinnerLocation, RelativeSource={RelativeSource TemplatedParent}}" Background="{TemplateBinding Background}" HorizontalContentAlignment="Stretch" IsTabStop="False" ShowButtonSpinner="{Binding ShowButtonSpinner, RelativeSource={RelativeSource TemplatedParent}}" VerticalContentAlignment="Stretch">
<xctk:WatermarkTextBox x:Name="PART_TextBox" Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" AutoMoveFocus="{Binding AutoMoveFocus, RelativeSource={RelativeSource TemplatedParent}}" AutoSelectBehavior="{Binding AutoSelectBehavior, RelativeSource={RelativeSource TemplatedParent}}" AcceptsReturn="False" BorderThickness="0" Background="Transparent" ContextMenu="{TemplateBinding ContextMenu}" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsTabStop="{TemplateBinding IsTabStop}" IsUndoEnabled="True" MinWidth="20" Padding="{TemplateBinding Padding}" SelectAllOnGotFocus="{Binding SelectAllOnGotFocus, RelativeSource={RelativeSource TemplatedParent}}" TextAlignment="{Binding TextAlignment, RelativeSource={RelativeSource TemplatedParent}}" TextWrapping="NoWrap" TabIndex="{TemplateBinding TabIndex}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" WatermarkTemplate="{Binding WatermarkTemplate, RelativeSource={RelativeSource TemplatedParent}}" Watermark="{Binding Watermark, RelativeSource={RelativeSource TemplatedParent}}"/>
</xctk:ButtonSpinner>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
,我可以这样使用它:
<xctk:DecimalUpDown Style="{StaticResource DecimalUpDownStyle1}" Value="{Binding DisplayValue, UpdateSourceTrigger=PropertyChanged}" />
我怀疑你的绑定参数在值转换中"丢失"了。NumericUpDown
控件内部通过TemplateBinding
将WatermarkTextBox
绑定到Text
属性,为了使控件尊重您的UpdateSourceTrigger,它可能需要在该级别应用。因此,由于这种中间绑定和Value
和Text
之间的非即时同步,您无法控制源代码更新行为。