c# - UWP 无法分配给自定义控件中的属性错误

标签 c# xaml win-universal-app

我有一个自定义控件 LineChart。 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); }
}

在 View 模型类中:

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

最佳答案

您在 DependencyProperty.Register 调用中犯了一个错误。第一个参数值应该是 "Stroke",而不是 "StrokeProperty":

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

关于c# - UWP 无法分配给自定义控件中的属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36259613/

相关文章:

c# - 我如何跳转到 Resharper 中的下一个引用?

c# - Windows 10 通用编译绑定(bind) (x :Bind) conflict with ViewModel

c# - 第二次打开控制台后尝试调用 Console.Clear() 时出现异常

c# - AutoMapper-从ViewModel映射回DTO时,subObject保持为空

c# - 如何从数据绑定(bind)中的转换器获取依赖属性

c# - WPF TreeView 项目不可选

xaml - 如何更改特定元素的 TextBox 占位符文本颜色,而不是全局

c# - 为什么在 Image 上旋转比使用 BitmapEncoder 快得多?

c# - Windows 通用项目不支持数据类型 "AnyName"(x :DataType for DataTemplate)

c# - Cassandra 准备好的语句的并发执行返回无效的 JSON 作为结果