Partager via


IXRPropertyBag (Compact 2013)

3/28/2014

This C++ base class provides methods that the XAML for Windows Embedded data binding engine uses to access or modify the value of a property of a data source object.

Syntax

class  IXRPropertyBag : public IUnknown

Inheritance Hierarchy

IUnknown

     IXRPropertyBag

Methods

Method

Description

IXRPropertyBag::GetPropertyChangedEvent

Retrieves the custom event that changes the value of the property in this property bag.

IXRPropertyBag::GetValue

Retrieves the value of the property of a data source object.

IXRPropertyBag::SetValue

Updates the value of the property of a data source object.

Thread Safety

Because IXRPropertyBag is a base class and users either use methods implemented in a derived class or implement the methods in a custom data source class, the methods are not thread safe. To ensure thread safety in a custom derived class, the derived method implementations can implement functionality to block other threads. For more information, see XRAutoCriticalSection.

Remarks

This class inherits from IUnknown.

If you prefer to develop custom functionality for managing data source properties and handling the PropertyChanged event, implement IXRPropertyBag on a data source object. If you prefer to use a property bag that works by default with the iXRPropertyBinding data source property implementation, provides GetValue and SetValue methods that you do not have to overwrite with custom implementations, and handles the PropertyChanged event, use TPropertyBag<Derived> instead.

One way to implement data binding for single-value elements is to implement a data source object that implements IXRPropertyBag.

To bind data from a data source object that implements IXRPropertyBag to a property of a XAML for Windows Embedded UI object, you can do the following:

  1. In the data source object, implement IXRPropertyBag.
  2. In the data source object, write a custom implementation of IXRPropertyBag::GetValue and/or IXRPropertyBag::SetValue.
  3. In the UI object, call IXRFrameworkElement::SetDataContext, and then supply your data source object as an XRValue of type VTYPE_PROPERTYBAG.
  4. In the UI object, call IXRFrameworkElement::SetBinding, and then supply the data flow direction and the property name.

Your data source object does not have to be a XAML for Windows Embedded object and does not have to inherit from IXRDependencyObject.

In order for a UI control to be used as the data source for another UI control, both controls must be data-bound to the same property in a data source object that implements IXRPropertyBag or a derived class, and one of the UI controls must support XRBindingMode_TwoWay.

When you create a class instance, use an IXRPropertyBagPtr smart pointer instead of a raw interface pointer. For more information, see XRPtr<Interface>.

For more information about the differences in data binding between Microsoft Silverlight 3 and XAML for Windows Embedded, see Differences Between Microsoft Silverlight 3 and XAML for Windows Embedded.

.NET Framework Equivalent

None.

Example

The following example code shows how to display the state of a radio by using an IXRToggleButton object in the UI that employs one-way and two-way data binding.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#include "windows.h"
#include "XamlRuntime.h"
#include "XRCustomEvent.h"
#include "XRPtr.h"


// The custom data source object that holds radio station state

class CRadioStation : public IXRPropertyBag
{
private:

     bool RadioState;
 IXRCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag> *pRadioEvent;
 

     
public:
     
void RadioStation()
     {
          RadioState = false;
  CreateEvent();
  
     }

virtual HRESULT STDMETHODCALLTYPE CreateEvent()
{

pRadioEvent = CreateCustomEvent<XRPropertyChangedCustomEventArgs, IXRPropertyBag>();
}

    virtual HRESULT STDMETHODCALLTYPE GetValue(__in const WCHAR* pstrPropertyName, __out XRValue *pValue)
     {
          HRESULT hr = E_FAIL;
          if (0 == wcscmp(pstrPropertyName, L"RadioState"))
          {
   XRValue propValue;
               propValue.vType = VTYPE_BOOL;
               propValue.BoolVal = RadioState;
   
               hr = S_OK;
          }
     }

     virtual HRESULT STDMETHODCALLTYPE SetValue(__in const WCHAR* pstrPropertyName, __in XRValue *pValue)
     {
          HRESULT hr = E_FAIL;
          if (0 == wcscmp(pstrPropertyName, L"RadioState"))
          {
               RadioState = pValue->BoolVal;
               XRPropertyChangedCustomEventArgs eventArgs;
               eventArgs.PropertyName = pstrPropertyName;
               pRadioEvent->Raise(this, &eventArgs);
               hr = S_OK;
               return hr;
          }
     }

};

void BindDataToControl(IXRToggleButton* pOptionButton, IXRApplication* pApplication)

{
    // Create the data source object 
     XRPtr<CRadioStation> DataSource;
     pApplication->CreateObject(&DataSource);

     // Convert the data source object to an XRValue
     XRValue value;
     value.vType = VTYPE_PROPERTYBAG;
     value.pPropertyBagVal = DataSource;

     // Set the data source object as the data context for the option button
     pOptionButton->SetDataContext(&value);
 
 // Set the binding value and source property
 XRBinding Binding;
 Binding.Mode = XRBindingMode_TwoWay;
 Binding.Path = L"RadioState";
 
 pOptionButton->SetBinding(L"IsChecked", &Binding); 
     
     // Bind the data-source property to the option button
     
     bool IsChecked;
     XRValue StateValue;
     
     StateValue.vType = VTYPE_BOOL;
     DataSource->GetValue(L"RadioState", &StateValue);
     IsChecked = StateValue.BoolVal;

     if (IsChecked)
     {
          pOptionButton->SetIsChecked(XRThreeState_Checked);

     }
     

     // Bind the user-updated value in option button to the data source

     XRThreeState checkedState;
     XRValue NewValue;
     NewValue.vType = VTYPE_BOOL;
     
    pOptionButton->GetIsChecked(&checkedState);
     

     if (checkedState == XRThreeState_Checked)
     {
          NewValue.BoolVal = true;
          DataSource->SetValue(L"RadioState", &NewValue);
     }
     
     if (checkedState == XRThreeState_Unchecked)
     {
          NewValue.BoolVal = false;
          DataSource->SetValue(L"RadioState", &NewValue);
     }

}

For more information about APIs used in this example, see IXRApplication, IXRToggleButton, XRValue, XRThreeState, IXRFrameworkElement::SetDataContext, and XRPropertyChangedCustomEventArgs.

Requirements

Header

XamlRuntime.h

sysgen

SYSGEN_XAML_RUNTIME

See Also

Reference

Classes for Populating UI Elements with Data
TPropertyBag<Derived>