custom attached property not found

丶灬走出姿态 提交于 2020-01-22 02:34:15

问题


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.

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.

PivotProperties.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

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

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>

... an error pops up, saying: "The attachable property 'MenuForeground' was not found in type 'PivotProperties'.

How to fix that?


回答1:


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

Please note also that it is not necessary that your PivotProperties class derives from DependencyObject, as long as it only declares attached properties.

You may also consider using Windows.UI.Xaml.Media.Brush as property type to make it conforming with other properties like Background and Foreground.



来源:https://stackoverflow.com/questions/29083858/custom-attached-property-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!