UWP Failed to assign to property error in custom control

不打扰是莪最后的温柔 提交于 2019-12-10 17:39:08

问题


I have a custom control LineChart. Code in xaml.cs:

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); }
}

In view model class:

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

In other page:

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

All works fine with static line of code Stroke="Green", but when I use Binding, program crushed with error:

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()


回答1:


You have made a mistake in your DependencyProperty.Register call. The first argument value should be "Stroke", not "StrokeProperty":

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


来源:https://stackoverflow.com/questions/36259613/uwp-failed-to-assign-to-property-error-in-custom-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!