2009-07-29 37 views
9

我在我的视图模型中有int类型的属性,该属性绑定到TextBox。一切工作正常,TwoWay绑定工作,细除了一种情况 -WPF绑定与int类型的属性无法正常工作

如果我明确的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"/> 
+0

我很想知道这种行为在silverlight中是否相同?任何人! – akjoshi 2010-09-03 09:41:59

+0

Silverlight中的行为与Silverlight中不支持的NotifyOnSourceUpdated和UpdateSourceTrigger之类的数据绑定属性除外。 – 2010-09-21 19:45:02

回答

25

我有类似的问题。

你只需要更新代码:

<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"/> 
6

这部分是一个猜测(我没有VS得心应手现在就试一试),但我认为这是因为一个清除文本框是空的string""),它不能被隐式转换为int。你应该实现一个类型转换器来为你提供转换。 (你可能想做点像转换“”到0)

+0

最简单的方法来检查 - 在Visual Studio中运行您的项目,并观察输出窗口(视图输出)。当你清除文本框时,你应该看到西蒙在谈论的绑定错误。 – 2009-07-29 10:28:28

+0

我有VS方便,你说得对。 +1 – 2009-07-29 10:31:05

0

这是因为int的值不能是null。 最好使用string属性,将您代码中的值转换为所需的int属性字段。

这样,你可以执行

if(string.IsNullOrEmpty(text)) 
{ 
    this.intValue = 0; 
} 
+0

而且投票原因是什么? – ChrisBD 2009-07-29 10:46:51

1

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> 

[注:这只是一个概念证明,为简单起见,我没有使用任何模式或最佳实践]

6

如果你不想使用Nullableinteger,你可以使用converter,其将空string为0,请参见下面的代码:

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 
} 
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; 
     } 
    } 
} 
相关问题