【问题标题】:UWP Failed to assign to property error in custom controlUWP 无法分配给自定义控件中的属性错误
【发布时间】:2016-03-28 09:37:49
【问题描述】:

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

在视图模型类中:

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:与关联的文本 找不到这个错误代码。

未能分配给属性 CurrencyExchangeUniversal.App.Controls.LineChart.Stroke'。 [行:102 位置:41] 在 Windows.UI.Xaml.Application.LoadComponent(Object 组件,Uri resourceLocator,ComponentResourceLocation 组件资源位置)在 CurrencyExchangeUniversal.App.View.NationalBankPage.InitializeComponent() 在 CurrencyExchangeUniversal.App.View.NationalBankPage..ctor()

【问题讨论】:

  • Abc的类型是什么?
  • Abc添加了代码
  • 似乎绑定是正确的。检查 OnItemsChanged 方法

标签: c# xaml win-universal-app


【解决方案1】:

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

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

【讨论】:

  • StackOverflow 上的一个问题经常发生在我身上:我没有犯与提问者相同的错误,但我仍然有同样的问题。由于这个问题已“回复”,我现在似乎没有找到解决方案:(
猜你喜欢
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
相关资源
最近更新 更多