代码之家  ›  专栏  ›  技术社区  ›  akjoshi HCP

WPF绑定不能正确处理int类型的属性

  •  9
  • akjoshi HCP  · 技术社区  · 17 年前

    我的财产是 int 键入绑定到的“我的视图”模型 TextBox . 一切正常, TwoWay 装订可以,但有一种情况除外-

    如果我清除了 ,虽然在中清除了值,但不会调用属性setter 文本框 ,属性仍保留上一个值。

    有没有人面临过类似的问题?有什么解决办法吗?

    这是物业-

    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"/>
    
    6 回复  |  直到 14 年前
        1
  •  25
  •   WPF User    16 年前

    我也有类似的问题。

    您只需将代码更新为:

    <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"/> 
    
        2
  •  6
  •   akjoshi HCP    11 年前

    如果你不想使用 Nullable integer ,你可以使用 converter 这将转换为空 string

    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
    }
    
        3
  •  6
  •   akjoshi HCP    11 年前

    这部分是猜测(我现在还没有VS handy来尝试),但我认为这是因为清除的文本框是空的 string ( "" int . 您可能应该实现一个类型转换器来为您提供转换。(您可能希望执行类似于将“”转换为0的操作)

        4
  •  1
  •   akjoshi HCP    11 年前

    您需要将integer属性更改为 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>
    

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

        5
  •  0
  •   akjoshi HCP    11 年前

    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;
           }
        }
    }
    
        6
  •  0
  •   akjoshi HCP    11 年前

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

    这样你就可以执行

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