我将文本框与类型为float的属性绑定。一切工作正常,我改变了TextBox的值,并在属性中更新。当我使TextBox变为空白时,我的属性没有得到更新,它仍然具有旧的价值。现在,我需要在绑定中使用转换器,以便在来自TextBox的空值的情况下使用默认值更新属性。我想知道为什么这种行为?还有其他解决方案吗?当文本框为空时,数据绑定不会更新
回答
你的财产没有更新,因为它不可能空字符串浮动转换。有两种方法可以解决这个问题。
第一种方法是添加一个类型为string的属性,将TextBox与它绑定并实现float属性的更改。就像这样:
public partial class Window1 : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Window1()
{
InitializeComponent();
// don't use this as DataContext,
// it's just an example
DataContext = this;
}
private float _FloatProperty;
public float FloatProperty
{
get { return _FloatProperty; }
set
{
_FloatProperty = value;
OnPropertyCahnged("FloatProperty");
}
}
private string _StringProperty;
public string StringProperty
{
get { return _StringProperty; }
set
{
_StringProperty = value;
float newFloatValue;
// I think you want 0 when TextBox is empty, right?
FloatProperty = float.TryParse(_StringProperty, out newFloatValue) ? newFloatValue : 0;
OnPropertyCahnged("StringProperty");
}
}
protected void OnPropertyCahnged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("StringProperty"));
}
}
}
第二种方法是使用转换器:
namespace WpfApplication3
{
public partial class Window1 : Window, INotifyPropertyChanged
{
public static readonly IValueConverter TextBoxConverter = new FloatConverter();
/* code from previous example without StringProperty */
}
public class FloatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
float f;
if (value is string && float.TryParse(value as string, out f))
{
return f;
}
return 0f;
}
}
}
XAML:
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication3="clr-namespace:WpfApplication3">
<Grid>
<TextBox Text="{Binding FloatProperty, Converter={x:Static WpfApplication3:Window1.TextBoxConverter}}" />
</Grid>
</Window>
我喜欢用MVVM pattern第一种方式。
我认为问题在于绑定系统匹配一个空的TextBox。对你而言,它可能为零,但对于其他人可能是Single.NegativeInfinity。
只是更改绑定这样
<TextBlock Text={Binding Path=Name, TargetNullValue='',UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}/>
谁从事这项工作,例如你已经将TargetNullValue设置为一个字符串而不是浮点数? –
这显然是在.net3.5 sp1中添加的。直到现在我还没有意识到这一点。干杯! – Daniel
好,保持工作伙伴 –
- 1. 当文本框更改而不是丢失焦点时更新数据绑定
- 2. 更改绑定对象时不会更新文本框集中
- 3. 双向数据绑定:更新不更新绑定属性的文本框
- 4. C#绑定文本框不会更新使用tableadapter更新
- 5. 文本块不更新数据绑定
- 6. 文本框不会更新数据库
- 7. 绑定的文本框文本不会更新
- 8. DataGrid在重新绑定新数据存储时不会更新
- 9. 清除文本框不会将绑定设置为空
- 10. WPF文本框绑定不会更新与拖放
- 11. 文本框绑定不会在RaisePropertychanged上更新
- 12. 文本框绑定的属性值不会立即更新
- 13. 文本框数据绑定
- 14. 数据绑定为两个文本框
- 15. 当绑定到函数时,UWP TextBlock不会更新
- 16. 数据绑定列表框将不会更新
- 17. 绑定复选框不会更新其数据源
- 18. 更改文本框到文本框丢失数据绑定 -
- 19. WPF数据绑定更新不及时
- 20. WPF数据绑定更新不及时
- 21. 数据绑定文本框不与源
- 22. 数据绑定不更新
- 23. 当值为空时,文本框“锁定”(预期为整数)
- 24. 当绑定项目被更改时,DataGridView不会更新?
- 25. 当DataSource更改时,DataGridRowGroupHeader中的Silverlight绑定不会更新
- 26. 使数据绑定输入文本框更新与AngularJS
- 27. 只有一个文本框更新与数据绑定源
- 28. WPF文本框绑定更新
- 29. 文本框绑定更新事件
- 30. 当绑定数据为空时,表单不显示
我明白了,谢谢你的回答! – viky