首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带状LostFocus + NumericUpDown问题

带状LostFocus + NumericUpDown问题
EN

Stack Overflow用户
提问于 2012-03-16 11:49:17
回答 1查看 1.1K关注 0票数 1

我正在使用扩展的WPF工具包,每当用户输入某些内容(PropertyChanged)时,我都想更新值。

但是,当使用IntegerUpDown (或任何NumericUpDown控件)时,该值只会在enter键按下或如果您按下控件上的向上按钮或该控件失去焦点时才会更新。如发展商所述,这是“按设计”进行的:

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

“这是由设计完成的。只有在输入键按下或失去焦点时,才会更新源值。”

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

DecimalUpDown (Extended WPF toolkit) - Source only gets updated on lost focus

然而,当我将它与丝带结合使用时,我遇到了严重的问题,当它被打开时,它实际上并不会触发LostFocus。因此,根据绑定到NumericUpDown的值在带中放置任何命令,都不会像预期的那样工作。

每当用户打开丝带时,我就想自己触发LostFocus事件,但这似乎很难看,而且可能不会一直工作。

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

有人找到解决办法了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-16 13:33:51

我已经通过重写控件的默认模板来解决这个问题。

代码语言:javascript
复制
<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>

我将文本属性的模板绑定更改为包含触发器。

代码语言:javascript
复制
  Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9736690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档