2014-09-04 95 views
1

有沒有辦法從編程生成的綁定中獲取錯誤消息?我只看到例如狀態「PathError」,但不是它失敗的原因。從WPF BindingExpression獲取錯誤消息

var binding = new Binding(path); 
var expression = BindingOperations.SetBinding(this, TestProperty, binding); 
if (expression.Status == BindingStatus.PathError) 
{ 
    throw new Exception("Invalid binding!"); //why did the binding fail? 
} 

依賴性屬性被定義爲

private static readonly DependencyProperty TestProperty = 
    DependencyProperty.Register("Test", typeof(object), typeof(DashboardShape)); 

亞歷

回答

0

,以查看Binding誤差WPF是在Visual Studio中,打開輸出窗口的方式。如果它們尚未輸出,則可以從選項對話框開啓WPF跟蹤到輸出窗口。您可以通過以下路徑找到的選項:一旦你找到相關選項頁面

工具>選項>調試>輸出窗口> WPF跟蹤設置

,您可以設置數據綁定設置爲除以外的其他值警告是一個很好的使用水平。

enter image description here


UPDATE >>>

Binding誤差應在輸出窗口顯示無論它們是如何創建的,所以它似乎很奇怪,你說有沒有任何。不過,我還有一個絕招,我喜歡嘗試調試Binding問題時用... ... DataBindingDebugConverter

public class DataBindingDebugConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     Debugger.Break(); 
     return value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     Debugger.Break(); 
     return value; 
    } 
} 

只需添加這ConverterBinding您有問題,然後你就可以查看您擁有的數據綁定值。請注意,這樣會出現而不是顯示Binding錯誤,但會幫助你調試你的Binding

+0

我已經將它設置爲Warning,但沒有顯示(大概是因爲綁定沒有在xaml代碼中設置 - 它是動態的)。此外,我正在尋找一種方法來查看代碼中的錯誤來記錄它。 – LionAM 2014-09-04 11:28:24