开发者

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

开发者 https://www.devze.com 2023-04-08 12:08 出处:网络
I am using Extended WPF toolkit\'s DecimalUpDown control with its Value property binded to a Decimal? as follows:

I am using Extended WPF toolkit's DecimalUpDown control with its Value property binded to a Decimal? as follows:

   <extToolkit:DecimalUpDown Value="{Binding BlahBlah, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ShowButtonSpinner="False" />

private Decimal? blahblah = 5;
    public Decimal? BlahBlah
    {
        get { return blahblah; }
        set { blahblah = value; }
    }

I noticed that as I key in the number in the textbox, the Value does开发者_StackOverflow中文版 not get updated until I click outside the control. Its ValueChanged event is not fired as well until I click outside.

I intend for the value to be updated as soon as the user changes the Value (i.e. real-time). Is there anyway to accomplish this?


Yes, you have to replace the control template, with one that has the UpdateSourceTrigger=PropertyChanged. I did this last year by copying the existing template, making the change, then using it in my control. New Resource:

        <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>

In my Control:

 Template="{StaticResource newDecimalUpDownTemplate}"


I myself also spent some time on this problem and found a pretty nice solution, I edited the template (Right click the DecimalUpDown and go to edit template Edit Template, thanks Ben you saved me some serious time --> How to overwrite a style) and combined it with the brilliant emorog solution!

I wrote all of this in a style:

<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>

and am able to use it like this:

<xctk:DecimalUpDown Style="{StaticResource DecimalUpDownStyle1}" Value="{Binding DisplayValue, UpdateSourceTrigger=PropertyChanged}" />


I suspect that your binding parameter "gets lost" in the value transitions. The NumericUpDown controls internally bind a WatermarkTextBox to the Text property via TemplateBinding, to have the control respect your UpdateSourceTrigger it would probably need to be applied at that level. So due to this intermediate binding and the non-immediate synching between Value and Text you cannot control the source-update-behavior.

0

精彩评论

暂无评论...
验证码 换一张
取 消