2011-02-15 3 views
1
에 라인 바인딩하려고 ErrorMessage가

임 두 ScatterViewItems에 라인을 결합하려고 :WPF : 두 UiElements의

private void BindLineToScatterViewItems(Shape line, ScatterViewItem origin, ScatterViewItem destination) 
     { 
      // Bind line.(X1,Y1) to origin.ActualCenter 
      BindingOperations.SetBinding(line, Line.X1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.X") }); 
      BindingOperations.SetBinding(line, Line.Y1Property, new Binding { Source = origin, Path = new PropertyPath("ActualCenter.Y") }); 

      // Bind line.(X2,Y2) to destination.ActualCenter 
      BindingOperations.SetBinding(line, Line.X2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.X") }); 
      BindingOperations.SetBinding(line, Line.Y2Property, new Binding { Source = destination, Path = new PropertyPath("ActualCenter.Y") }); 
     } 

하지만 항상 다음과 같은 오류 메시지가 얻을 : 그럼에도 불구하고 그것을

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='NaN' BindingExpression:Path=ActualCenter.X; DataItem='ScatterViewItem' (Name=''); target element is 'Line' (Name=''); target property is 'X1' (type 'Double')

을 작동하지만 어떻게 경고를 무시할 수 있습니까? 그리고이 경고가 표시되는 이유는 무엇입니까?

편집 : 아래의 답변에 따르면, 지금 계산기 다음,하지만 여전히 얻을 사용 오류 :

public class NormalizationConverter : IValueConverter 

    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return (double) value == double.NaN ? Binding.DoNothing : (double) value; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

답변

2

나는 표면을 가지고 있지만 분명히 ActualCenter.X하고 할당하기 전에 double.NaN로 시작 ActualCenter.Y하지 않습니다 그들의 실제 가치. 이것이 오래 지속되지 않기 때문에 다른 이중 값을 대신 사용할 수 있습니다.

public class NormalizationConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var doubleValue = (double)value; 
     return doubleValue == double.NaN ? 0 : doubleValue; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

내가 이러한 경우에 Binding.DoNothing를 사용하는 부분이야 : 그래서 경고를 피하기 위해, 당신은 double.NaNO에 변환하는 컨버터를 사용할 수 있습니다. – Goran

+0

@ 골란 : 좋은 제안. –

+0

@Goran 어떻게해야합니까? – RoflcoptrException