【问题标题】:Ribbon LostFocus + NumericUpDown issues功能区 LostFocus + NumericUpDown 问题
【发布时间】:2012-03-16 11:49:17
【问题描述】:

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

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

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

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

将 UpdateSourceTrigger 设置为任何值(即 PropertyChanged)均无效,此 SO 线程中也记录了这一点:

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

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

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

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

有人找到解决办法了吗?

【问题讨论】:

  • 问题是您不知道用户何时停止在文本框中输入内容。很难以优雅的方式解决。
  • @HansPassant 我不会尝试 - 我想在值发生变化时更新属性,就像它与 TextBox 一样

标签: c# wpf ribboncontrolslibrary


【解决方案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}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多