【问题标题】:DecimalUpDown (Extended WPF toolkit) - Source only gets updated on lost focusDecimalUpDown(扩展 WPF 工具包)- 源仅在失去焦点时更新
【发布时间】:2011-09-27 10:58:53
【问题描述】:

我正在使用扩展 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; }
    }

我注意到,当我在文本框中输入数字时,值不会更新,直到我在控件外部单击。在我单击外部之前,它的 ValueChanged 事件也不会被触发。

我打算在用户更改值后立即更新值(即实时)。有没有办法做到这一点?

【问题讨论】:

  • 我本来想说你应该将 UpdateSourceTrigger 设置为 PropertyChanged 但你已经这样做了。

标签: c# wpf


【解决方案1】:

是的,您必须将控件模板替换为具有 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}"

【讨论】:

    【解决方案2】:

    我自己也花了一些时间解决这个问题并找到了一个非常好的解决方案,我编辑了模板(右键单击 DecimalUpDown 并转到编辑模板编辑模板,谢谢 Ben,你为我节省了一些时间 --> How to overwrite a style ) 并将其与出色的 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}" />
    

    【讨论】:

      【解决方案3】:

      我怀疑您的绑定参数在值转换中“丢失”了。 NumericUpDown 控件通过TemplateBinding 在内部将WatermarkTextBox 绑定到Text 属性,以使控件尊重您的UpdateSourceTrigger,它可能需要在该级别应用。因此,由于这种中间绑定以及ValueText 之间的非立即同步,您无法控制源更新行为。

      【讨论】:

      • 这是正确的。目前该值只会在 LostFocus 或按下 Enter 键时更新。
      • 我想我对此无能为力。谁能推荐另一个可以满足我要求的 WPF 组件(免费或商业)?
      • 您可以下载源代码并实现您需要的功能。
      • @BrianLagunas 也偶然发现了这个问题。您至少应该向 codeplex 站点添加对此行为的描述。否则,我希望 UpdateSourceTrigger 能够像其他所有控件一样工作。这也是非常有问题的,因为有时 LostFocus 根本不会被调用(例如按下功能区上的按钮时)
      猜你喜欢
      • 1970-01-01
      • 2018-08-20
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2018-07-05
      • 1970-01-01
      相关资源
      最近更新 更多