locked
Dependency property of type int

    Question

  • I've found how to define a dependency property which has an object type: http://msdn.microsoft.com/en-us/library/windows/apps/hh920267.aspx

    But I don't know how to define an integer dependency property in C++.

    Especially, how can I get the typeid of type int (or IntPtr maybe?):

    DependencyProperty^ MyControl::_myPropProperty = 
        DependencyProperty::Register("MyProp", 
        /*Platform::IntPtr::typeid*/int::typeid, // ??
        MyControl::typeid, 
        nullptr);

    Note that documentation for method DependencyProperty::Register doesn't have a C++ sample.

    Friday, June 29, 2012 1:26 AM

Answers

  • What you have there with int::typeid should work. You can find C++ samples of DependencyProperty::Register in the Xaml user and custom controls sample. I added an integral property to its ImageWithLabelControl control:

    DependencyProperty^ ImageWithLabelControl::_NumberProperty = DependencyProperty::Register("Number", int::typeid, ImageWithLabelControl::typeid, nullptr);
    
    --Rob

     

    Friday, June 29, 2012 2:23 AM
    Owner

All replies

  • What you have there with int::typeid should work. You can find C++ samples of DependencyProperty::Register in the Xaml user and custom controls sample. I added an integral property to its ImageWithLabelControl control:

    DependencyProperty^ ImageWithLabelControl::_NumberProperty = DependencyProperty::Register("Number", int::typeid, ImageWithLabelControl::typeid, nullptr);
    
    --Rob

     

    Friday, June 29, 2012 2:23 AM
    Owner
  • Thanks, this works fine for int dependency properties.

    So I've tried to go further and create a dependency property of type enum.

    enum myEnum {...};
    
    public ref myClass sealed : public Control
    {
    internal:
        property myEnum myProp
        {
            myEnum get() { return (myEnum)(int)GetValue (myEnumProperty); }
            void set(myEnum value) { SetValue((int)myEnumProperty, value); }
        }
    };
    

    I have to make my property internal since my enum is a native type. But myEnum is an integer, isn't it? So it might be enabled? Or not? IMHO the documentation of C++/CX classes does not give enough information about this limitation.

    Also, the compiler doesn't accept conversions of dependency property types (Platform::Object^) to enum. So I inserted an additional (int) cast. And I get this error messages when trying to cast the dependency property to (int) type:

    error C2440: 'type cast': cannot convert from 'Windows::UI::Xaml::DependencyProperty ^' to 'int' 
        A type representing an address in the gc heap can only be converted to integral type via pinning, or 
        No standard conversion exists from the boxed form of the arithmetic type to the target type.

    OK, the compiler doesn't accept enum dependency properties. But I'm a bit confused about all this.

    Is it safe to use dependency properties of type 'int'? As far as I have tried, it is. But I'm not sure the compiler agrees 100%.

    Monday, July 02, 2012 2:04 AM
  • (Replying to myself)

    OK, integral dependency properties work great.

    However it seems it is not possible to use enum-type dependency properties. Instead I use an property of type int, and cast its value.

    Tuesday, July 10, 2012 11:42 PM