locked
Define a dependency property in C++

    Question

  • I'm trying to define a dependency property for a user control in C++, but with no success.

    I have this user control called UnitValueControl, which is basically a stack panel with two text blocks. I want to add some bindings for the Text property of the text blocks, so I'm trying to define some dependency properties. Here is how the header looks:

     

    namespace UnitConverter
    {
        public ref class UnitValueControl sealed
        {
    		private:
    			static Windows::UI::Xaml::DependencyProperty^ UnitProperty = 
    				Windows::UI::Xaml::DependencyProperty::Register(L"Unit", L"Platform::String", L"UnitConverter::UnitValueControl", nullptr);	
    
            public:
                UnitValueControl();
                ~UnitValueControl();
    
    			property Platform::String^ Unit
    			{
    				Platform::String^ get()
    				{
    					return getUnit();
    				}
    				void set(Platform::String^ value)
    				{
    					setUnit(value);
    				}
    			}
    
    	private:
    		Platform::String^ getUnit();
    		void setUnit(Platform::String^ value);
        };
    }
    
    
    and the source file

     

     

    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Data;
    using namespace UnitConverter;
    
    UnitValueControl::UnitValueControl()
    {
        InitializeComponent();
    }
    
    UnitValueControl::~UnitValueControl()
    {
    }
    
    Platform::String^ UnitValueControl::getUnit()
    {
    	return (Platform::String^)GetValue(UnitProperty);
    }
    
    void UnitValueControl::setUnit(Platform::String^ value)
    {
    	SetValue(UnitProperty, value);
    }
    

     


    Doesn't work. The designer does not show the unit control when adding it to another control (I only get an icon with an exclamation mark in a yellow circle). Running the app triggers some exception:

     

    First-chance exception at 0x5F26DE32 (Windows.UI.Xaml.dll) in UnitConverter.exe: 0xC0000005: Access violation reading location 0x00000004.

     

    So here are some questions:

    • are the parameters to DependencyProperty::Register pass correctly? should I use . instead of :: ("Platform.String")? Should I use the hat ("Platform::String^")?
    • why if I put the code of the get/set setters of the property in the header (instead of calling getUnit()/setUnit()) I get linker errors for multiple definition of UnitProperty?
    UnitValueControl.g.obj : error LNK2005: "private: static class Windows::UI::Xaml::DependencyProperty ^ ^ UnitConverter::UnitValueControl::UnitProperty" (?UnitProperty@UnitValueControl@UnitConverter@@0P$AAVDependencyProperty@Xaml@UI@Windows@@$AA) already defined in UnitValueControl.xaml.obj
    XamlTypeInfo.g.obj : error LNK2005: "private: static class Windows::UI::Xaml::DependencyProperty ^ ^ UnitConverter::UnitValueControl::UnitProperty" (?UnitProperty@UnitValueControl@UnitConverter@@0P$AAVDependencyProperty@Xaml@UI@Windows@@$AA) already defined in UnitValueControl.xaml.obj
    C:\Work\UnitConverter\Debug\UnitConverter\UnitConverter.exe : fatal error LNK1169: one or more multiply defined symbols found

    Is there any sample for how do write something like this? The pack with samples from code.msdn.com does not have any.


    Microsoft MVP VC++ | www.mariusbancila.ro/blog | www.codexpert.ro
    Sunday, September 25, 2011 6:47 AM

All replies

  • Hi Marius,

     

    I am looking into this one. 

     

    thanks for the patience,

    mike

    Tuesday, September 27, 2011 8:04 PM
    Moderator
  • Hi Mike,

     

    Me too waiting the way to declare the Dependency property in Metro style applications using C++.

     

    Regards,

    David C

    Friday, September 30, 2011 4:21 AM
  • This is how we do it....

    In the header of SomeColorControl:

     public:

    // Static DependencyProperties

    static property Windows::UI::Xaml::DependencyProperty^ StringProperty

    {

         Windows::UI::Xaml::DependencyProperty^ get()

         {

              return _stringProperty;

         }

    }

        public:

    // Public properties

    property String^ SomeString

    {

         String ^ get()

         {

              return (String^)GetValue(StringProperty);

         }

         void set(String ^ value)

         {

              SetValue(StringProperty, value);

         }

    }

    In the CPP file:

    DependencyProperty^ SomeTextControl::_stringProperty = DependencyProperty::Register(

        "Name",

        GetTypeName<Platform::String>(), // T::typeid->name + metadata

    GetTypeName<SomeTextControl>(),

        ref new PropertyMetadata(nullptr,

    ref new propertyChangedCallback(&Control::OnTextPropertyChanged)));


    • Edited by Activeghost Saturday, October 13, 2012 2:40 AM
    • Proposed as answer by Can BilginMVP Monday, October 22, 2012 8:19 AM
    Saturday, October 13, 2012 2:39 AM