2014-01-21 3 views
1

null을 전송할 방법이 있습니까? 그렇게하려고하면 0으로 간주됩니다. Business-Class 및 SQL-Table에서는 속성이 nullable입니다. Im는 ClearSelectionButton과 함께 ComboBox를 사용하므로 이미 뷰에서이를 수정하는 방법이있을 수 있습니다. 보기에null이 아닌 0 - int로 설정

내 콤보

   <telerik:RadComboBox x:Name="CommandButton" ItemsSource="{Binding Path=ColorList}" 
            SelectedValue="{Binding Path=Model.Color, Converter={StaticResource VCShortIntToInt}, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" 
            DisplayMemberPath="Text" SelectedValuePath="number" ClearSelectionButtonVisibility="Visible" ClearSelectionButtonContent="empty" /> 

비즈니스 클래스

public static PropertyInfo<short?> ColorProperty = RegisterProperty<short?>(c=>c.Color); 
    public short? Color 
    { 
     get { return GetProperty<short?>(ColorProperty); } 
     set { SetProperty<short?>(ColorProperty, value); } 
    } 

변환기

public class VCShortIntToInt : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     Int32 result = 0; 
     if (value != null && value.GetType() == typeof(Int16)) 
     { 
      result = System.Convert.ToInt32(value); 
     } 
     return result; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     Int16 result = 0; 
     if (value != null && value.GetType() == typeof(Int32)) 
     { 
      result = System.Convert.ToInt16(value); 
     } 
     return result; 
    } 
} 

답변

3

null을 전송하는 방법이 있나요 나의 재산? 내가 그렇게하려고하면, 그것은 입력이 null 때 당신의 컨버터가 0을 반환하기 때문에입니다 0

로합니다. 당신의 ConvertBack 방법에서 보면 (코멘트 나 추가) 적이 :

Int16 result = 0;  // result is initialized to 0 

    // Since `value` is `null`, the if branch is not taken 
    if (value != null && value.GetType() == typeof(Int32)) 
    { 
     result = System.Convert.ToInt16(value); 
    } 

    return result;   // 0 is returned. 

해결책은 간단하다 : 그냥 "널 (NULL)"반환 값을 유지 :

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    return (Int16?)value; 
}