Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绑定到Xaml(Windows 8.1)中的自定义属性_Xaml_Windows Runtime_Winrt Xaml_C++ Cx - Fatal编程技术网

绑定到Xaml(Windows 8.1)中的自定义属性

绑定到Xaml(Windows 8.1)中的自定义属性,xaml,windows-runtime,winrt-xaml,c++-cx,Xaml,Windows Runtime,Winrt Xaml,C++ Cx,我正在尝试设置一个自定义的附加属性,我可以在Xaml中绑定到该属性。如果我将普通字符串传递给它,它可以正常工作: MyProperties.h: #pragma once #include "pch.h" namespace SimpleApp { namespace WUX = Windows::UI::Xaml; public ref class MyProperties sealed : public WUX::DependencyObject { p

我正在尝试设置一个自定义的附加属性,我可以在Xaml中绑定到该属性。如果我将普通字符串传递给它,它可以正常工作:

MyProperties.h:

#pragma once

#include "pch.h"

namespace SimpleApp
{
    namespace WUX = Windows::UI::Xaml;

    public ref class MyProperties sealed : public WUX::DependencyObject
    {
    private:
        static WUX::DependencyProperty^ _MyValue;

    public:
        MyProperties::MyProperties();

        static property WUX::DependencyProperty^ MyValue
        {
            WUX::DependencyProperty^ get()
            {
                return _MyValue;
            }
        };
        static Platform::String^ GetMyValue(WUX::UIElement^ element);
        static void SetMyValue(WUX::UIElement^ element, Platform::String^ value);
    };
}
MyProperties.cpp:

#include "pch.h"
#include "MyProperties.h"

using namespace Windows::UI::Xaml;
using namespace SimpleApp;

MyProperties::MyProperties()
{
};


DependencyProperty^ MyProperties::_MyValue = DependencyProperty::RegisterAttached(
    "MyValue", 
    Platform::String::typeid, 
    MyProperties::typeid,
    ref new PropertyMetadata(false)
    );

Platform::String^ MyProperties::GetMyValue(UIElement^ element)
{
    auto val = safe_cast<Platform::String^>(element->GetValue(_MyValue));
    return val;
}

void MyProperties::SetMyValue(UIElement^ element, Platform::String^ stringValue)
{
    element->SetValue(_MyValue, stringValue);
    OutputDebugString(L"It worked!");
    // Also do other stuff
}
Xaml:

这是可行的-SetMyValue函数在执行时传递了Hello参数

但是,当我尝试在MyValue属性上使用绑定时,SetMyValue不会执行

<TextBlock local:MyProperties.MyValue="{Binding Width}" Text="{Binding Width}" />
我也将文本绑定到宽度,只是为了确保它在一般情况下是有效的。我是否需要在某个地方做出一个神奇的声明来说服属性是可绑定的


NB提出了类似的要求,但对于WPF,询问者似乎没有解决他的问题。

我认为您的绑定正在工作。没有调用SetMyValue这一事实对我来说也很有意义。WPF绕过了您的属性getter/setter,而是使用内部依赖性属性,因为性能原因Seg SetMyValue不是打电话来

如果您对价值变化感兴趣,您需要明确地说:

随机复制/粘贴

  public static readonly DependencyProperty FilterIDProperty = 
          DependencyProperty.RegisterAttached(
     FilterIDPropertyName,
     typeof(int),
     typeof(PeopleUserControl),
     new FrameworkPropertyMetadata(0, 
          FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, FilterIDChanged));
一旦依赖项属性发生更改,就会调用FilterIDCHanged。另外,请注意,默认情况下,我将BindStwOway传递给依赖项属性,如果需要,我不记得100%,但这也可能是绑定不起作用的原因

  public static readonly DependencyProperty FilterIDProperty = 
          DependencyProperty.RegisterAttached(
     FilterIDPropertyName,
     typeof(int),
     typeof(PeopleUserControl),
     new FrameworkPropertyMetadata(0, 
          FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, FilterIDChanged));