尝试这样做的方式,我设置任何其他属性不工作,因为我理解,但不知道如何工作的方式。所以我们有一些具体的讨论,这里有一些(无效)XAML。我已经减少了一切尽可能多的 – 原来有属性,如设计师的大小,但我相信那些是与我想要做的无关。
<Page x:Class="WpfSandBox.TestPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MyProperty="MyPropertyValue"></Page>
和相应的代码隐藏:
using System.windows.Controls;namespace WpfSandBox { public partial class TestPage : Page { public TestPage() { InitializeComponent(); } public string MyProperty { get; set; } }}
错误信息:
Error 1 The property ‘MyProperty’ does not exist in XML namespace
‘07000’. line 4 position 7.
现在我知道为什么这是失败:元素是类型的页面,并且页面没有属性名为MyProperty。这只是在TestPage …中声明的,它是由x:Class属性指定的,而不是由元素本身指定的。据我所知,这种配置是XAML处理模型(即Visual Studio集成等)所必需的。
我怀疑我可以处理这个与依赖属性,但感觉有点像过度。我也可以使用现有的属性(例如DataContext),然后将该值复制到代码中的自定义属性中,但这将是非常丑陋。
上面是一个WPF示例,但我怀疑相同的答案将适用于Silverlight。我对这两个感兴趣 – 所以如果你发布一个答案,你知道将工作在一个,但不是其他,我将不胜感激,如果你在答案内指出:
我准备踢自己,当有人发布一个绝对琐碎的解决方案…
解决方法 如果为您的页面创建了Base类,则可以使用normal属性而不使用Dependency属性。public class BaseWindow : Window { public string MyProperty { get; set; } }<local:BaseWindow x:Class="Basewindowsample.Window1" x:name="winImp" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Basewindowsample" MyProperty="myproperty value" title="Window1" Height="300" WIDth="300"></local:BaseWindow>
并且它工作,即使MyProperty不是依赖或附加。
总结以上是内存溢出为你收集整理的在WPF/Silverlight页面中设置自定义属性全部内容,希望文章能够帮你解决在WPF/Silverlight页面中设置自定义属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)