Monday, 15 February 2010

Binding to a custom property in Xaml (Windows 8.1 -



Binding to a custom property in Xaml (Windows 8.1 -

i'm trying set custom attached property can bind in xaml. works fine if pass plain strings:

myproperties.h:

#pragma 1 time #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() { homecoming _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)); homecoming val; } void myproperties::setmyvalue(uielement^ element, platform::string^ stringvalue) { element->setvalue(_myvalue, stringvalue); outputdebugstring(l"it worked!"); // other stuff }

xaml:

<page x:class="simpleapp.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:simpleapp" width="525" > <grid x:name="_grid" width="500" datacontext="{binding elementname=_grid}"> <textblock local:myproperties.myvalue="hello" text="{binding width}" /> </grid> </page>

this works - setmyvalue function executes parameter "hello" passed it.

when seek , utilize binding on myvalue property, though, setmyvalue doesn't execute. i'm replacing textblock in xaml:

<textblock local:myproperties.myvalue="{binding width}" text="{binding width}" />

i binding text width sure is, in general, working. there magic declaration have create somewhere persuade property bindable?

nb this question asking similar, wpf, , asker didn't seem have problem solved anyway.

i think binding working. fact setmyvalue not called makes sense me. wpf bypasses property getter/setter, , uses internal dependency property instead, performance reasons(eg setmyvalue not called).

if interested value changes, need explictly so:

(random copy/paste)

public static readonly dependencyproperty filteridproperty = dependencyproperty.registerattached( filteridpropertyname, typeof(int), typeof(peopleusercontrol), new frameworkpropertymetadata(0, frameworkpropertymetadataoptions.bindstwowaybydefault, filteridchanged));

filteridchanged called, 1 time dependency property changes. also, notice passed bindstwowaybydefault dependency property, don't remember 100% if that's needed, can reason why binding doesn't work.

xaml windows-runtime winrt-xaml c++-cx

No comments:

Post a Comment