[英]WPF binding not working properly with properties of int type
我在我的视图模型中有一个int
类型的属性,它绑定到TextBox
。 一切正常, TwoWay
绑定工作正常,除了一个案例 -
如果我清除TextBox
的值,则不会调用属性setter,虽然在TextBox
清除了值,但属性仍保留以前的值。
有没有人遇到类似的问题? 这有什么解决方法吗?
这是物业 -
public int MaxOccurrences
{
get
{
return this.maxOccurrences;
}
set
{
if (this.maxOccurrences != value)
{
this.maxOccurrences = value;
base.RaisePropertyChanged("MaxOccurrences");
}
}
}
这是我如何绑定xaml中的属性 -
<TextBox Text="{Binding Path=MaxOccurrences, Mode=TwoWay,
NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Center" Width="30" Margin="0,0,5,0"/>
我有类似的问题。
您只需将代码更新为:
<TextBox Text="{Binding Path=MaxOccurrences, Mode=TwoWay, TargetNullValue={x:Static sys:String.Empty},
NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Center" Width="30" Margin="0,0,5,0"/>
这部分是一个猜测(我现在没有得到VS方便试试),但我认为这是因为一个清除的文本框是一个空string
( ""
),它不能隐式转换为int
。 您可能应该实现类型转换器来为您提供转换。 (你可能想做一些像转换“”到0的东西)
如果您不想使用Nullable
integer
,可以使用将空string
转换为0的converter
,请参阅下面的代码:
public class EmptyStringToZeroConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null || string.IsNullOrEmpty(value.ToString())
? 0
: value;
}
#endregion
}
Akjoshi,我有一个有效的解决方案!
您需要将整数属性更改为Naullable<int>
(即int?
),请参阅以下代码段:
private int? _maxOccurrences;
public int? MaxOccurrences
{
get { return _maxOccurrences; }
set { _maxOccurrences = value; }
}
您还需要添加值转换器以将空字符串转换为空值,请参阅以下代码段:
public class EmptyStringToNullConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null || string.IsNullOrEmpty(value.ToString())
? null
: value;
}
#endregion
}
XAML代码:
<Window x:Class="ProgGridSelection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:ProgGridSelection"
Title="MainWindow"
Height="136" Width="525"
Loaded="OnWindowLoaded">
<Window.Resources>
<local:EmptyStringToNullConverter x:Key="EmptyStringToNullConverter"/>
</Window.Resources>
<StackPanel>
<Button Content="Using Empty String to Null Converter"/>
<TextBox Name="empNameTextBox"
Text="{Binding Mode=TwoWay, Path=MaxOccurrences,
RelativeSource={RelativeSource FindAncestor, AncestorType=Window},
Converter={StaticResource EmptyStringToNullConverter}}"/>
</StackPanel>
[注意:这只是概念的证明,为了简单起见,我没有使用任何模式或最佳实践]
这是因为int
值不能为null
。 最好使用string
属性将代码中的值转换为所需的int属性字段。
这样你就可以执行一个
if(string.IsNullOrEmpty(text))
{
this.intValue = 0;
}
我有同样的问题,但我需要处理而不是数值。 我使用以下转换器:
public class StringFormatToIntConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is string)
{
var inttext =
System.Text.RegularExpressions.Regex.Replace((string)value, "[^.0-9]", "");
int number;
return Int32.TryParse(inttext, out number) ? number : 0;
}
else
{
return value;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.