1

我正在使用扩展的 WPF 工具包,并且希望在用户输入内容时更新值(PropertyChanged

但是,当使用 IntegerUpDown(或任何 NumericUpDown 控件)时,该值将仅在按下回车键或按下控件上的向上向下按钮或控件失去焦点时更新。正如开发人员在此处所述,这是“设计使然”:

http://wpftoolkit.codeplex.com/workitem/16544

“这是设计使然。源只会在值增加/减少、按 Enter 键或失去焦点时更新。”

将 UpdateSourceTrigger 设置为任何值(即PropertyChanged)都没有效果,这也记录在这个 SO 线程中:

DecimalUpDown(扩展 WPF 工具包)- 源仅在失去焦点时更新

但是,当我将它与功能区结合使用时,我遇到了严重的问题,当它打开时,它实际上并没有触发 LostFocus。因此,根据绑定到 NumericUpDown 的值将任何命令放入功能区将永远不会按预期工作。

每当用户打开功能区时,我都在考虑自己触发 LostFocus 事件,但这似乎很难看,并且可能不会一直有效。

我正在寻找一个优雅的解决方案来解决这个问题,我真的不想下载源代码并修改它。我要么想修改 NumericUpDown 控件以实际更新PropertyChanged上的值(这可以仅使用额外的 XAML / 行为 / 触发器来完成吗?)或者找到一种优雅的方式来触发 LostFocus 事件,只要我想不实际破坏用户体验.

有没有人找到解决方案?

4

1 回答 1

1

我通过覆盖控件的默认模板解决了这个问题

<Style x:Key="EnhancedNumericUpDown" TargetType="Control">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Control">
                    <ext:ButtonSpinner x:Name="Spinner"
                                    IsTabStop="False"
                                    Background="{TemplateBinding Background}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    AllowSpin="{Binding AllowSpin, RelativeSource={RelativeSource TemplatedParent}}"
                                    ShowButtonSpinner="{Binding ShowButtonSpinner, RelativeSource={RelativeSource TemplatedParent}}">
                        <ext:WatermarkTextBox x:Name="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}"
                                          IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"
                                          MinWidth="20"
                                          AcceptsReturn="False"
                                          Padding="0"
                                          SelectAllOnGotFocus="{Binding SelectAllOnGotFocus, RelativeSource={RelativeSource TemplatedParent}}"
                                          TextAlignment="{Binding TextAlignment, RelativeSource={RelativeSource TemplatedParent}}"
                                          TextWrapping="NoWrap" 
                                          TabIndex="{TemplateBinding TabIndex}"
                                          Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
                                          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Watermark="{Binding Watermark, RelativeSource={RelativeSource TemplatedParent}}"
                                          WatermarkTemplate="{Binding WatermarkTemplate, RelativeSource={RelativeSource TemplatedParent}}" />
                    </ext:ButtonSpinner>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我更改了 Text 属性的模板绑定以包含触发器

  Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
于 2012-03-16T13:33:51.297 回答