2016-03-28 73 views
3

我有自定義控件LineChart。代碼xaml.cs:UWP未能在自定義控件中分配屬性錯誤

public static readonly DependencyProperty StrokeProperty = 
     DependencyProperty.Register("StrokeProperty", typeof(Brush), typeof(LineChart), new PropertyMetadata(new SolidColorBrush(), 
      new PropertyChangedCallback(OnItemsChanged))); 

public Brush Stroke 
{ 
    get { return (Brush)GetValue(StrokeProperty); } 
    set { SetValue(StrokeProperty, value); } 
} 

在視圖模型類:

public Brush Abc 
{ 
    get { return new SolidColorBrush(new Color() {A = 123, B = 123, G = 23, R = 12 }); } 
} 

在其他頁面:

<controls:LineChart Stroke="{Binding Abc}" /> 

所有工作正常的代碼Stroke="Green"靜線,但是當我使用Binding,有錯誤的程序粉碎:

Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.

Failed to assign to property CurrencyExchangeUniversal.App.Controls.LineChart.Stroke'. [Line: 102 Position: 41] at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation) at CurrencyExchangeUniversal.App.View.NationalBankPage.InitializeComponent() at CurrencyExchangeUniversal.App.View.NationalBankPage..ctor()

+0

什麼是ABC的類型? – Archana

+0

爲'Abc'添加代碼 –

+0

看起來像綁定是正確的。檢查OnItemsChanged方法 – Archana

回答

3

您在撥打DependencyProperty.Register時犯了錯誤。第一個參數值應爲"Stroke",不"StrokeProperty"

public static readonly DependencyProperty StrokeProperty = 
     DependencyProperty.Register("Stroke", typeof(Brush), typeof(LineChart), new PropertyMetadata(new SolidColorBrush(), 
      new PropertyChangedCallback(OnItemsChanged))); 
+0

StackOverflow上的一個問題常常發生在我身上:我沒有和提問者犯同樣的錯誤,而且我仍然有同樣的問題。由於這個問題是「回答」,我現在似乎沒有找到解決方案:( –