The fix was .NET Framework 4.5 or 4.51
(did not work properly with 3.5 or 3.5 client)
TextBox Ignoring Value Returned By Get
-
22-09-2022 - |
题
It appears the TextBox is ignoring the value returned by the get if it decides the value did not change but the value actually did change
In this example value is limited to 0-6 in the set
In the real application this range is assigned by the business layer
Managing it with keydown event is not my preference
Enter a 7 and the TextBox resets to 6 just fine
Problem is enter 6 followed by any other number and the TextBox does not reset
The TextBlock (not TextBox) does reset (displays 6)
For example enter 65 and will have 65 in the TextBox and 6 in the TextBox
I see the get called twice
I see 6 coming from the converter but 65 is displayed
Was .NET 4.0 Visual Studio 2010
According to @ethicallogics this problem does not happen Visual Studio 2012
Upgraded to Visual Studio 2013 and and I still will have the problem
Target Framework is set to 4.0 Client Profile
Even added a converter and I can see the 6 (not 65) is being sent to the TextBox
Changed to Target Framework to 4.51 and it works correctly (also works correctly on 4.5)
<Window x:Class="TextBoxMax.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TextBoxMax"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:IntStrConverter x:Key="strConverter"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox Text="{Binding Path=Inum, Converter={StaticResource strConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="0" Width="80" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBlock Text="{Binding Path=Inum, Mode=OneWay}"
Grid.Row="1" Width="80" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,0,0,0"/>
</Grid>
</Window>
namespace TextBoxMax
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private int inum = 0;
private string str = string.Empty;
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
}
public int Inum
{
get
{
return inum;
}
set
{
if (inum == value) return;
inum = value;
if (inum < 0) inum = 0;
if (inum > 6) inum = 6;
NotifyPropertyChanged("Inum");
}
}
}
[ValueConversion(typeof(Int32), typeof(string))]
public class IntStrConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Int32 i = (Int32)value;
return i.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value as string;
Int32 i;
if (Int32.TryParse(strValue, out i))
{
return i;
}
return DependencyProperty.UnsetValue;
}
}
}
解决方案