[英]UWP InvalidCastException when using XAML databinding
谁能解释如何评估XAML数据绑定表达式? 我有一个带有注册属性VisualState的控件。
public CardStates VisualState
{
get
{
return (CardStates)this.GetValue(VisualStateProperty);
}
set
{
this.SetValue(VisualStateProperty, value);
}
}
public static readonly DependencyProperty VisualStateProperty = DependencyProperty.RegisterAttached("VisualStateProperty", typeof(CardStates), typeof(StateManager), new PropertyMetadata(null, (s, e) => { }));
在xaml中,我尝试将值绑定到此属性。 状态存在于父级的DataContext对象中。
<local:CardControl VisualState="{Binding State.Value}" />
XamlTypeInfo.g.cs中生成的代码如下所示
private void set_4_CardControl_VisualState(object instance, object Value)
{
var that = (global::MeetEric.UI.Controls.CardControl)instance;
that.VisualState = (global::MeetEric.ViewModels.CardStates)Value;
}
此代码引发InvalidCastException,因为Value的值是Windows.UI.Xaml.Data.Binding对象。
我是否缺少明显的东西来启用数据绑定? 我需要某种形式的转换器吗?
尝试将typeof(StateManager)
更改为typeof(CardControl)
。 (位于DependencyProperty.RegisterAttached)
此参数需要DependencyProperty的所有者。
当您尝试将Binding与常规属性一起使用时,会发生此错误。 为了使绑定解析为正确的类型,您需要将其绑定到DependencyProperty
。
正如其他人提到的那样,您的语法有些偏离,这可能会在这里引起问题。
此错误肯定是非常神秘的。 我只是在自己的项目中找到了它,但是一旦我将其属性更改为一个依赖项,它就可以完美工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.