在WPF / Silverlight页面中设置自定义属性 [英] Setting a custom property within a WPF/Silverlight page

查看:107
本文介绍了在WPF / Silverlight页面中设置自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来应该很简单。我有一个以正常方式在XAML中声明的 Page (即带有添加新项...),并且它具有自定义属性。我想在与页面关联的XAML中设置该属性。

This sounds like it should be simple. I have a Page declared in XAML in the normal way (i.e. with "Add new item...") and it has a custom property. I'd like to set that property in the XAML associated with the page.

尝试以与设置任何其他属性相同的方式进行操作,出于某种原因,我了解但不知道如何解决。顺便说一句,我们有一些具体的要讨论的东西,这里是一些(无效的)XAML。我已尽可能减少所有内容-最初具有诸如设计师大小之类的属性,但我认为这些属性与我要执行的操作无关。

Trying to do this the same way that I'd set any other property doesn't work, for reasons I understand but don't know how to work round. Just so we've got something concrete to talk about, here's some (invalid) XAML. I've reduced everything down as much as possible - originally there were attributes such as the designer size, but I believe those are irrelevant to what I'm trying to do.

<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; }
  }
}

错误消息:


错误1属性'MyProperty'在XML名称空间
' http://schemas.microsoft.com/winfx/2006/xaml/presentation 。第4行的位置7。

现在我知道为什么会失败:元素的类型为 Page Page 没有名为 MyProperty 的属性。只能在 TestPage ...中声明,该属性由 x:Class 属性指定,而不由元素本身指定。据我所知,XAML处理模型(即Visual Studio集成等)需要此配置。

Now I know why this is failing: the element is of type Page, and Page doesn't have a property called MyProperty. That's only declared in TestPage... which is specified by the x:Class attribute, but not by the element itself. As far as I'm aware, this configuration is required by the XAML processing model (i.e. the Visual Studio integration etc).

我怀疑我可以使用依赖项属性来处理此问题,但这有点像过大杀伤力。我还可以使用现有的属性(例如 DataContext ),然后稍后在代码中将值复制到自定义属性中,但这非常丑陋。

I suspect I could handle this with a dependency property, but that feels a little like overkill. I could also use an existing property (e.g. DataContext) and then copy the value into the custom property in code later, but that would be pretty ugly.

上面是一个WPF示例,但我怀疑在Silverlight中也适用相同的答案。我对两者都感兴趣-因此,如果您发布知道可以在一个答案中解决而不是在另一个答案中起作用的答案,那么如果您在答案中指出这一点,我将不胜感激:)

The above is a WPF example, but I suspect the same answers will apply in Silverlight. I'm interested in both - so if you post an answer which you know will work in one but not the other, I'd be grateful if you'd indicate that within the answer :)

当有人发布绝对微不足道的解决方案时,我正准备踢自己一脚...

I'm preparing to kick myself when someone posts an absolutely trivial solution...

推荐答案

如果为页面创建基类,则可以使用不带依赖项属性的普通属性。

You can work with normal property without Dependency property if you create a Base class for your Page.

 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不是依赖项或附加属性,它也可以工作。

And it works even though MyProperty is not a Dependency or Attached.

这篇关于在WPF / Silverlight页面中设置自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆