[英]WPF binding not working properly with properties of int type
I am having a property of int
type in my view model which is bound to a TextBox
. 我在我的视图模型中有一个
int
类型的属性,它绑定到TextBox
。 Everything works properly, TwoWay
binding works fine except in one case - 一切正常,
TwoWay
绑定工作正常,除了一个案例 -
If I clear the value of TextBox
, property setter doesn't gets called and although value is cleared in TextBox
, property still holds the previous value. 如果我清除
TextBox
的值,则不会调用属性setter,虽然在TextBox
清除了值,但属性仍保留以前的值。
has anyone faced similar issue? 有没有人遇到类似的问题? is there any workaround for this?
这有什么解决方法吗?
Here is the property - 这是物业 -
public int MaxOccurrences
{
get
{
return this.maxOccurrences;
}
set
{
if (this.maxOccurrences != value)
{
this.maxOccurrences = value;
base.RaisePropertyChanged("MaxOccurrences");
}
}
}
Here is how I am binding the property in xaml - 这是我如何绑定xaml中的属性 -
<TextBox Text="{Binding Path=MaxOccurrences, Mode=TwoWay,
NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Center" Width="30" Margin="0,0,5,0"/>
I had the similar problem. 我有类似的问题。
You just need to update the code as: 您只需将代码更新为:
<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"/>
This is partially a guess (I haven't got VS handy right now to try it out), but I think it's because a cleared text box is an empty string
( ""
), which can't be implicitly converted to an int
. 这部分是一个猜测(我现在没有得到VS方便试试),但我认为这是因为一个清除的文本框是一个空
string
( ""
),它不能隐式转换为int
。 You should probably implemented a type converter to provide the conversion for you. 您可能应该实现类型转换器来为您提供转换。 (you probably want to do something like convert "" to 0)
(你可能想做一些像转换“”到0的东西)
If you don't want to use a Nullable
integer
, you can use a converter
that converts the empty string
to 0, see the code below: 如果您不想使用
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, I have a working solution! Akjoshi,我有一个有效的解决方案!
You need to change your integer property to Naullable<int>
(ie int?
), see the following snippet: 您需要将整数属性更改为
Naullable<int>
(即int?
),请参阅以下代码段:
private int? _maxOccurrences;
public int? MaxOccurrences
{
get { return _maxOccurrences; }
set { _maxOccurrences = value; }
}
You also need to add a value converter to convert the empty string to null value, see the following code snippet: 您还需要添加值转换器以将空字符串转换为空值,请参阅以下代码段:
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
}
The XAML code: 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>
[Note: this is just a proof of concept, and for the sake of simplicity I didn't use any patterns or best practices] [注意:这只是概念的证明,为了简单起见,我没有使用任何模式或最佳实践]
It's because an int
value can't be null
. 这是因为
int
值不能为null
。 It's best to use a string
property that converts the value for you within your code to the required int property field. 最好使用
string
属性将代码中的值转换为所需的int属性字段。
That way you can perform a 这样你就可以执行一个
if(string.IsNullOrEmpty(text))
{
this.intValue = 0;
}
I have the same problem, but I need to handle and not numeric value to. 我有同样的问题,但我需要处理而不是数值。 I use the following converter for this:
我使用以下转换器:
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.