未找到自定义附加属性 [英] custom attached property not found
问题描述
我想为枢轴元素实现一个新属性(名为MenuForeground"),以便通过定义的 ControlTemplate 更改 PivotItem 标头的颜色.
I want to implement a new property (named "MenuForeground") for the pivot element, in order change the color of the PivotItem header through a defined ControlTemplate.
因此我为自定义属性创建了一个新类,在需要的代码隐藏 xaml.h 文件中添加了 #include,并根据自定义属性的命名空间定义了一个新的命名空间(xamlns:cap").
Therefore I created a new class for the custom property, added the #include in the needed code-behind xaml.h file and defined a new namespace ("xamlns:cap") according to the namespace of the custom property.
枢轴属性.h
#pragma once
using namespace Windows::UI::Xaml;
namespace CustomAttachedProperties
{
public ref class PivotProperties sealed : Windows::UI::Xaml::DependencyObject
{
public:
static Windows::UI::Color GetMenuForeground(UIElement^ obj);
static void SetMenuForeground(UIElement^ obj, Windows::UI::Color value);
static property DependencyProperty^ MenuForegroundProperty
{
DependencyProperty^ get() { return _menuForegroundProperty; }
}
private:
static DependencyProperty^ _menuForegroundProperty;
};
}
PivotProperties.cpp
PivotProperties.cpp
#include "pch.h"
#include "PivotProperties.h"
using namespace CustomAttachedProperties;
DependencyProperty^ PivotProperties::_menuForegroundProperty = DependencyProperty::RegisterAttached(
"MenuForeground",
Windows::UI::Color::typeid,
Windows::UI::Xaml::Controls::Pivot::typeid,
ref new PropertyMetadata(false));
Windows::UI::Color PivotProperties::GetMenuForeground(UIElement^ obj)
{
return (Windows::UI::Color)obj->GetValue(_menuForegroundProperty);
}
void PivotProperties::SetMenuForeground(UIElement^ obj, Windows::UI::Color value)
{
obj->SetValue(_menuForegroundProperty, value);
}
为了将新属性用于枢轴元素,我在根元素中声明了一个新的 xml 命名空间,如下所示
In order to use the new property for a pivot element I declared a new xml namespace in the root element like the following
<Page
// ...
xmlns:cap="clr-namespace:CustomAttachedProperties">
但是如果我尝试使用新属性...
But if I try to use the new property ...
<Pivot x:Name="pivot" cap:PivotProperties.MenuForeground="Red">...</Pivot>
... 弹出一个错误,说:在‘PivotProperties’类型中找不到可附加属性‘MenuForeground’.
... an error pops up, saying: "The attachable property 'MenuForeground' was not found in type 'PivotProperties'.
如何解决这个问题?
推荐答案
RegisterAttached
方法必须是
The third parameter ownerType
of the RegisterAttached
method must be
注册依赖属性的所有者类型
The owner type that is registering the dependency property
不是要设置属性的对象的类型.
not the type of the object where you want to set the property.
所以你的声明应该是这样的:
So your declaration should look like this:
DependencyProperty^ PivotProperties::_menuForegroundProperty =
DependencyProperty::RegisterAttached(
"MenuForeground",
Windows::UI::Color::typeid,
PivotProperties::typeid, // here
ref new PropertyMetadata(false));
<小时>
另请注意,您的 PivotProperties
类不必从 DependencyObject
派生,只要它只声明附加属性即可.
Please note also that it is not necessary that your PivotProperties
class derives from DependencyObject
, as long as it only declares attached properties.
您也可以考虑使用 Windows.UI.Xaml.Media.Brush
作为属性类型,使其符合其他属性,例如 Background
和 Foreground代码>.
You may also consider using Windows.UI.Xaml.Media.Brush
as property type to make it conforming with other properties like Background
and Foreground
.
这篇关于未找到自定义附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!