I was able to get a data binding that works between the property of IsCheckedtwo elements ToggleButtonbased on the insanely bad example found in the help files that ship with CTP WCE7. I would expect to establish a data context and data bindings in XAML, but in the documentation I was asked to code it.
First you need to create a class that implements IXRPropertyBag. Then you must set the data context and data binding to the instance of this property package from the code.
I'm sorry that the code does not have a naming convention, but it is still much better than the example provided by Microsoft. It is also not as simple as it can be, but I will leave this refactoring to you.
MyPropertyBag.h:
#pragma once
#include "windows.h"
#include "XamlRuntime.h"
#include "XRCustomEvent.h"
#include "XRPtr.h"
class __declspec(uuid("3C6FFC6F-17A8-4976-B034-B4FE3BFF530A"))
MyPropertyBag : public IXRPropertyBag
{
private:
LONG m_cRef;
IXRCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag> *pRadioEvent;
XRThreeState RadioState;
public:
MyPropertyBag(void);
HRESULT STDMETHODCALLTYPE GetValue(__in const WCHAR* pstrPropertyName, __out XRValue *pValue);
HRESULT STDMETHODCALLTYPE SetValue(__in const WCHAR* pstrPropertyName, __in XRValue *pValue);
HRESULT STDMETHODCALLTYPE GetPropertyChangedEvent(__out IXRCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag>** ppEvent);
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID * ppvObj);
ULONG STDMETHODCALLTYPE AddRef();
ULONG STDMETHODCALLTYPE Release();
};
MyPropertyBag.cpp:
#include "StdAfx.h"
#include "MyPropertyBag.h"
extern "C" const GUID __declspec(selectany) IID_MyPropertyBag = __uuidof(IXRPropertyBag);
MyPropertyBag::MyPropertyBag(void)
{
RadioState = XRThreeState_Unchecked;
pRadioEvent = CreateCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag>();
}
HRESULT MyPropertyBag::GetValue(__in const WCHAR* pstrPropertyName, __out XRValue *pValue)
{
HRESULT hr = E_FAIL;
if (0 == wcscmp(pstrPropertyName, L"RadioState"))
{
pValue->vType = VTYPE_INT;
pValue->IntVal = (int)RadioState;
hr = S_OK;
}
return hr;
}
HRESULT MyPropertyBag::SetValue(__in const WCHAR* pstrPropertyName, __in XRValue *pValue)
{
HRESULT hr = E_FAIL;
if (0 == wcscmp(pstrPropertyName, L"RadioState"))
{
RadioState = (XRThreeState)pValue->IntVal;
XRPropertyChangedCustomEventArgs eventArgs;
eventArgs.PropertyName = pstrPropertyName;
pRadioEvent->Raise(this, &eventArgs);
hr = S_OK;
}
return hr;
}
HRESULT MyPropertyBag::GetPropertyChangedEvent(IXRCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag>** ppEvent)
{
*ppEvent = pRadioEvent;
return S_OK;
}
HRESULT MyPropertyBag::QueryInterface(REFIID riid, LPVOID * ppvObj)
{
if (!ppvObj)
return E_INVALIDARG;
*ppvObj = NULL;
if (riid == IID_IUnknown || riid == IID_MyPropertyBag)
{
*ppvObj = (LPVOID)this;
AddRef();
return NOERROR;
}
return E_NOINTERFACE;
}
ULONG MyPropertyBag::AddRef()
{
InterlockedIncrement(&m_cRef);
return m_cRef;
}
ULONG MyPropertyBag::Release()
{
ULONG ulRefCount = InterlockedDecrement(&m_cRef);
if (0 == m_cRef)
{
delete this;
}
return ulRefCount;
}
BindDataToControl StartDialog :
XRPtr<MyPropertyBag> viewModel(new MyPropertyBag());
BindDataToControl(pTb1, viewModel);
BindDataToControl(pTb2, viewModel);
hr = m_pVisualHost->StartDialog(&uiExitCode);
SetWinMainResultCode(uiExitCode);
BindDataToControl, DataContext :
inline void App::BindDataToControl(IXRFrameworkElement* pElement, IXRPropertyBag* pPropertyBag)
{
XRBinding binding;
binding.Mode = XRBindingMode_TwoWay;
binding.Path = L"RadioState";
pElement->SetBinding(L"IsChecked", &binding);
XRValue dataContext;
dataContext.vType = VTYPE_PROPERTYBAG;
dataContext.pPropertyBagVal = pPropertyBag;
pElement->SetDataContext(&dataContext);
}