我正在使用Extended WPF工具包的DecimalUpDown控件,其Value属性绑定到Decimal?如下:
<extToolkit:DecimalUpDown Value="{Binding BlahBlah, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ShowButtonSpinner="False" />
private Decimal? blahblah = 5;
public Decimal? BlahBlah
{
get { return blahblah; }
set { blahblah = value; }
}
我注意到,当我键入文本框中的数字时,在我点击控件外部之前,Value不会更新。在我点击外面之前,它的ValueChanged事件也不会被触发。
我打算在用户更改Value(即实时)后立即更新值。无论如何要实现这个目标吗?
答案 0 :(得分:2)
是的,您必须使用具有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>
在我的控制中:
Template="{StaticResource newDecimalUpDownTemplate}"
答案 1 :(得分:2)
我自己也花了一些时间来解决这个问题,并找到了一个非常好的解决方案,我编辑了模板(右键单击DecimalUpDown并转到编辑模板编辑模板,感谢Ben你为我节省了一些时间 - &gt; {{ 3}})并将它与精彩的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}" />
答案 2 :(得分:1)
我怀疑你的绑定参数在值转换中“迷失”。 NumericUpDown
控件通过WatermarkTextBox
在内部将Text
绑定到TemplateBinding
属性,以使控件尊重您可能需要在该级别应用的UpdateSourceTrigger。因此,由于此中间绑定和Value
与Text
之间的非直接同步,您无法控制源更新行为。