XAML 中没有“属性”一词的附加属性;

发布于 2024-09-27 17:06:59 字数 236 浏览 9 评论 0原文

MS 定义了“Grid.RowProperty”和“Grid.ColumnProperty”等附加属性,但在 XAML 中,您只需使用“Grid.Row”和“Grid.Column”来调用它,

我尝试对名为“MyValProperty”的附加属性执行相同的操作在“Foo”类上以名称“MyVal”注册,但 XAML 不允许我输入“Foo.MyVal”,而是让我输入“Foo.MyValProperty” MS 是如何做到这一点的?我缺少什么?

MS defines attached properties like 'Grid.RowProperty' and 'Grid.ColumnProperty', but in XAML you just call it with 'Grid.Row' and 'Grid.Column'

I tried doing the same with an attached property called 'MyValProperty' that was registered with the name 'MyVal' on the class 'Foo', but the XAML won't let me type 'Foo.MyVal' and instead makes me type 'Foo.MyValProperty' How is MS doing that? What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-10-04 17:06:59

作为背景,以下是如何注册附加属性

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
    "IsBubbleSource",
    typeof(Boolean),
    typeof(AquariumObject),
    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}

您还可以使用标准属性格式而不是 Set/Get 构造。这是 WPF 拥有强大约定的领域之一。 AttachedProperty(或任何DependencyProperty)由三个部分组成。第一个是使用 DependencyProperty.RegisterAttached 注册属性,并且返回值应设置为名为 [Property Name]Property 的公共静态变量。第二个是注册财产时的第一个参数应该是“[财产名称]”。第三个是用于与外界交互的 Get/Set 方法,应命名为 Get[Property Name]、Set[Property Name]。

如果您按照约定进行设置,WPF 将识别这两者是连接的,并且应该允许您按预期使用该属性。

As background here's how register an attached property

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
    "IsBubbleSource",
    typeof(Boolean),
    typeof(AquariumObject),
    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}

You can also use a standard property format rather than the Set/Get construct. This is one of those areas where WPF has a strong convention in place. There are three parts of an AttachedProperty (or any DependencyProperty). The first is registering the property with DependencyProperty.RegisterAttached and the return value should be set to a public static variable named [Property Name]Property. The second is that the first argument when registering your property should be "[Property Name]". And the third is the Get/Set methods you'll use to interact with the outside world which should be named Get[Property Name], Set[Property Name].

If you set it up according to the convention WPF will recognize that the two are connected and should allow you to use the property as you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文