xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.91k stars 878 forks source link

Why the UpdateSourceTrigger=Propertychanged dose not work correctly in IntegerUpdown? #1656

Open abdou31 opened 3 years ago

abdou31 commented 3 years ago

My question is based on How to set a restriction to WPF IntegerUpDown toolkit? that I've posted a while ago.

I have two WPF IntegerUpdowncontrols, one represents the maximum number and the other represents the minimum number , the first should be greater than the second and the second should be less than the first.

The given solution by @D M in the first question solve my problem

 <wpfToolkit:IntegerUpDown x:Name="minimumatt" 
                          Value="0" 
                          Minimum="0" />

<wpfToolkit:IntegerUpDown x:Name="maximumatt" 
                          Value="0" 
                          Minimum="0" 
                          Maximum="{Binding ElementName=minimumatt, 
                                            Path=Value
                                            UpdateSourceTrigger=PropertyChanged}" />

But this can be only one when I click outside the control , the scenario that should be achieved is when the user selects a number in minimum IntegerUpdowncontrol greater than the number that already exist in maximum , the number should be set to 0 or default when the user lost focus by mouse at that moment.

For example , in this case when the user put 18 in min , the value should be changed in lost focus event, what worked now is that the user has to click outside this control to get the default value in minimum IntegerUpdown

image

I found an event (Mouseleave_event) that can be used to update the value in real time ( without clicking on other controls ) but I did not have any idea how can I use this event with Propertychanged ,

How can I do this?

XceedBoucherS commented 3 years ago

Hi,

Yes, with your solution, typing "1", then "8" in the second IntergerUpDown, followed by a click outside or press Enter, will result in displaying the last valid number, which is "1". What exactly do you want to do ? As soon as, let's say the "8" is typed, you want to replace the IntegerUpDown.Value with a 0 because it exceeds the maximum allowed ? Wouldn't that be irritating for users typing the first digits and seeing all typed digits removed as soon as it exceeds the Maximum allowed, not letting them fix their typo ?

abdou31 commented 3 years ago

I would like to make this runing in real-time not whé the user click outside the control becsuse , in this case will display the same valur when the user click on button confirm but no the real value displayed .

XceedBoucherS commented 3 years ago

If you set the property ClipValueToMinMax to true, as soon as a value greater than the maximum is typed, then the maximum value will be set as the current value.

You could also redefine the IntegerUpDown to override the OnTextChanged method to force a "0" when the value exceeds the Maximum allowed: ` public class MyIntegerUpDown : IntegerUpDown { protected override void OnTextChanged( string oldValue, string newValue ) { int result; if( Int32.TryParse( newValue, out result ) ) { if( result > this.Maximum ) { this.Dispatcher.BeginInvoke( DispatcherPriority.Input, new Action( () => { this.Text = "0"; } ) ); return; } }

  base.OnTextChanged( oldValue, newValue );
}

}`