【发布时间】:2013-12-27 18:09:24
【问题描述】:
如果 TextBox 确定值没有改变但值确实改变了,它似乎忽略了 get 返回的值
在此示例中,集合中的值限制为 0-6
在实际应用中,此范围由业务层分配
使用 keydown 事件管理它不是我的偏好
输入7,TextBox重置为6就好了
问题是输入 6 后跟任何其他数字,并且 TextBox 不会重置
TextBlock(不是 TextBox)会重置(显示 6)
例如输入 65,TextBox 中有 65,TextBox 中有 6
我看到两次被调用
我看到 6 来自转换器,但显示的是 65
是 .NET 4.0 Visual Studio 2010
根据@ethicallogics 这个问题不会发生 Visual Studio 2012
升级到 Visual Studio 2013,我仍然会遇到问题
目标框架设置为 4.0 客户端配置文件
甚至添加了一个转换器,我可以看到 6(不是 65)正在发送到 TextBox
已将目标框架更改为 4.51,它可以正常工作(在 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;
}
}
}
【问题讨论】:
-
我复制并粘贴了代码,它不允许输入 65 。文本框和文本块都显示 6。
-
@ethicallogics 谢谢,我在 .NET 4.0 和 Visual Studio 2010 上,并在 2 台不同的计算机上重现了这个问题。您使用的是哪个版本的 .NET?字符串 > mmm 也有同样的问题。
-
.Net 4.0 和 VS 2012 。我尝试通过不同的方式复制它,例如复制和粘贴 65 ,并在 65 之间输入不正确的字符,然后删除那些不正确的格式字符,并且所有工作正常.
-
现在我已经在 VS 2010 中尝试过了。这个问题是可重现的,并且可以进入 65
-
@ethicallogics 谢谢,希望有 VS 2010 修复。
标签: .net wpf inotifypropertychanged