2008-10-24 76 views
2

使用數據綁定時,如何綁定使用值類型的新對象?.Net DataBinding具有值類型屬性的新對象

簡單的例子:

public class Person() { 
    private string _firstName; 
    private DateTime _birthdate; 
    private int _favoriteNumber; 
    //Properties 
} 

如果我創建一個新的Person(),並將其綁定到文本框的窗體。出生日期顯示爲01/01/0001,收藏號碼爲0.這些字段是必需的,但我希望這些框爲空,並讓用戶填寫。

該解決方案還需要能夠默認領域。在我們的例子中,我可能希望最喜歡的數字默認爲42.

我特意詢問Silverlight,但我認爲WPF和WinForms可能具有相同的問題。

編輯:

我認爲可空類型的,但我們目前正在使用的客戶端和服務器的同一域中的對象,我不希望有需要的字段可爲空。我希望數據綁定引擎公開一種方法來知道它綁定了一個新的對象?

回答

2

也許你可以嘗試可空類型?

public class Person() { 
    private string? _firstName; 
    private DateTime? _birthdate; 
    private int? _favoriteNumber; 
    //Properties 
} 

public class Person() { 
    private Nullable<string> _firstName; 
    private Nullable<DateTime> _birthdate; 
    private Nullable<int> _favoriteNumber; 
    //Properties 
} 

這實際上是相同的。

現在,默認值爲null,您可以通過設置屬性來強制設置值。

更多關於可空類型:

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

0

我已經重寫了Person類看起來更像這個...

public class Person 
    { 
    private int _favoriteNumber = 0; 
    public string FavoriteNumber 
    { 
     get 
     { 
     return _favoriteNumber > 0 ? _favoriteNumber.ToString() : string.Empty; 
     } 
     set 
     { 
     _favoriteNumber = Convert.ToInt32(value); 
     } 
    } 

    private DateTime _birthDate = DateTime.MinValue; 
    private string BirthDate 
    { 
     get 
     { 
     return _birthDate == DateTime.MinValue ? string.Empty : _birthDate.ToString(); //or _birthDate.ToShortDateString() etc etc 
     } 
     set 
     { 
     _birthDate = DateTime.Parse(value); 
     } 
    } 
    } 
0

可以使用的IValueConverter來格式化文本根據對象的值綁定到默認值。下面是對的IValueConverter

http://ascendedguard.com/2007/08/data-binding-with-value-converters.html http://weblogs.asp.net/marianor/archive/2007/09/18/using-ivalueconverter-to-format-binding-values-in-wpf.aspx

幾個環節不幸的是,這可能不是你所需要的,因爲你不具備這些特性的空值的選項。

你可以做的是在做數據綁定時爲你的對象設置默認屬性。

您可以通過將Person.Empty對象設置爲默認值來實現此目的。或者在設置DataContext時顯式設置這些值。

無論哪種方式應該工作雖然:)

1

嘗試使用值轉換器,這裏是應該讓你開始了一個例子。

基本的想法是當數據顯示時將類型的默認值轉換爲null,並且在更新綁定源時將任何空值轉換回類型默認值。

public class DefaultValueToNullConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     object result = value; 
     Type valueType = parameter as Type; 

     if (value != null && valueType != null && value.Equals(defautValue(valueType))) 
     { 
      result = null; 
     } 

     return result; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     object result = value; 
     Type valueType = parameter as Type; 

     if (value == null && valueType != null) 
     { 
      result = defautValue(valueType); 
     } 
     return result; 
    } 

    private object defautValue(Type type) 
    { 
     object result = null; 
     if (type == typeof(int)) 
     { 
      result = 0; 
     } 
     else if (type == typeof(DateTime)) 
     { 
      result = DateTime.MinValue; 
     } 
     return result; 
    } 
} 

然後在你的XAML中引用該轉換器這樣

<Page.Resources> 
    <local:DefaultValueToNullConverter x:Key="DefaultValueToNullConverter"/> 
</Page.Resources> 

<TextBox 
    Text="{Binding 
      Path=BirthDate, 
      Converter={StaticResource DefaultValueToNullConverter}, 
      ConverterParameter={x:Type sys:DateTime}}" 
    /> 
0

後,你有你的轉換器的地方,還需要impliment INotifyPropertyChanged的Person對象上。這樣,您可以設置綁定的Mode = TwoWay雙向數據綁定,當對文本框進行更改時,數據綁定將更新對象中的值並與vis相比較。

相關問題