C# 如何将xaml属性绑定到来自扩展的DependencyProperty
我想在C# 如何将xaml属性绑定到来自扩展的DependencyProperty,c#,xaml,data-binding,uwp,dependency-properties,C#,Xaml,Data Binding,Uwp,Dependency Properties,我想在FrameworkElement的扩展类中创建一个DependencyProperty,并将xaml属性绑定到它 这个想法来自Windows社区工具包的源代码: 他们正在使用扩展名向FrameworkElementExtensions.ActualSize.cs文件中的FrameworkElement添加一个DependencyProperty 我可以毫无问题地使用它们的扩展,但是当我自己尝试做同样的事情时,我得到一个Windows.UI.Xaml.Markup.XamlParseExce
FrameworkElement
的扩展类中创建一个DependencyProperty
,并将xaml属性绑定到它
这个想法来自Windows社区工具包的源代码:
他们正在使用扩展名向FrameworkElementExtensions.ActualSize.cs文件中的FrameworkElement
添加一个DependencyProperty
我可以毫无问题地使用它们的扩展,但是当我自己尝试做同样的事情时,我得到一个Windows.UI.Xaml.Markup.XamlParseException:“Xaml解析失败”。
我甚至尝试复制/粘贴他们的扩展类,但问题仍然存在
我的项目是UWP项目
下面是一些简单的代码来测试它:
ActualHeight属性是从Windows社区工具包复制/粘贴的
这是我的财产
第一个网格中的赋值工作正常,但是绑定到第二个网格中的高度和不透明度会引发异常
我看不出当从社区工具包名称空间而不是从我的名称空间使用时,它如何工作
例外情况的详细信息: 类型:
Windows.UI.Xaml.Markup.XamlParseException
信息:
XAML解析失败。
堆栈跟踪:
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
at MyProject.Controls.MyControl.InitializeComponent()
at MyProject.Controls.MyControl..ctor()
at MyProject.MyProject_XamlTypeInfo.XamlTypeInfoProvider.Activate_271_MyControl()
at MyProject.MyProject_XamlTypeInfo.XamlUserType.ActivateInstance()
InnerException为空
有人能帮我吗
谢谢。多亏了canton7的洞察力,我修复了它 问题来自
dependencProperty
声明
替换
public static readonly DependencyProperty CustomPropertyProperty = DependencyProperty.RegisterAttached("CustomProperty", typeof(double), typeof(FrameworkElement), new PropertyMetadata(1.0));
与
这里的关键是
DependencyProperty.RegisterAttached()
中的ownerType
参数。它需要是扩展类型,而不是扩展类型。“抛出异常”--什么异常?消息和堆栈跟踪是什么?@canton7我添加了异常和内部异常的详细信息?目前,明显的错误是您的UserControl
有两个子项,而它只支持一个子项。但是,如果没有完整的异常信息(包括InnerException和它们的InnerException,这将说明XAML解析是如何失败的),就无法确定了。@canton7子项问题是因为我想简化文章的示例。已经修好了。innerException为空@我试过了,但问题仍然存在。问题是,如果我只使用toolkitExtensions
public static readonly DependencyProperty CustomPropertyProperty = DependencyProperty.RegisterAttached("CustomProperty", typeof(double), typeof(FrameworkElement), new PropertyMetadata(1.0));
public static readonly DependencyProperty CustomPropertyProperty = DependencyProperty.RegisterAttached("CustomProperty", typeof(double), typeof(FrameworkElementExtensions), new PropertyMetadata(1.0));