2016-03-28 53 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上的一个问题常常发生在我身上:我没有和提问者犯同样的错误,而且我仍然有同样的问题。由于这个问题是“回答”,我现在似乎没有找到解决方案:( –