locked
Bind to inherited property

    Question

  • Hi,

    I'm trying to bind to the "Size"-property of a IObservableVector, which is inherited from IVector, using XAML. Is this possible? It doesn't seem to work.

    Best Regards,

    Liechtenschwein

    Friday, August 08, 2014 12:53 PM

Answers

  • Since you are using a binding converter you could bind to the IObservableVector itself, defining the binding converter similarly to the following:

        public ref class VectorSizeToVisibility sealed : Windows::UI::Xaml::Data::IValueConverter
        {
        public:
            VectorSizeToVisibility(){}
    
            virtual Platform::Object^ Convert(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType,
                Platform::Object^ parameter, Platform::String^ language)
            {
                auto v1 = safe_cast<IObservableVector<Answer^>^>(value);
                if (v1->Size == 0)
                    return Visibility::Visible;
                else
                    return Visibility::Collapsed;
            }
    
            virtual Platform::Object^ ConvertBack(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType,
                Platform::Object^ parameter, Platform::String^ language)
            {
                throw ref new Platform::NotImplementedException();
            }
    
        private:
            ~VectorSizeToVisibility(){}
        };

    Where, instead of Answer^ you put in your own type.

    If I had to speculate I would say not being able to bind to size is by design.  The issue with allowing binding to IObservableVector->Size is that the binding might have to be re-evaluated every time the Vector contents were modified.  So if you had a loop adding a million items to the vector you would see the binding evaluated a million times.

    Sunday, August 17, 2014 5:50 PM

All replies

  • Hi Liechtenschwein,

    Size for IVector is read-only, how could you assign a number for it? And what is your scenario?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, August 11, 2014 9:20 AM
    Moderator
  • I don't want to assign a number for it.

    I want to bind the visibility-property of a control to the size property of the IObservableVector that is binded to a CollectionViewSource, using a converter that converts a size of 0 to Visible and every other value to Collapsed. Everything works fine except I can't bind to the Size-property, the Output says IObservaleVector doesn't have this property, eventhough it should be inheritted from IVector.

    Tuesday, August 12, 2014 12:28 PM
  • Since you are using a binding converter you could bind to the IObservableVector itself, defining the binding converter similarly to the following:

        public ref class VectorSizeToVisibility sealed : Windows::UI::Xaml::Data::IValueConverter
        {
        public:
            VectorSizeToVisibility(){}
    
            virtual Platform::Object^ Convert(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType,
                Platform::Object^ parameter, Platform::String^ language)
            {
                auto v1 = safe_cast<IObservableVector<Answer^>^>(value);
                if (v1->Size == 0)
                    return Visibility::Visible;
                else
                    return Visibility::Collapsed;
            }
    
            virtual Platform::Object^ ConvertBack(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType,
                Platform::Object^ parameter, Platform::String^ language)
            {
                throw ref new Platform::NotImplementedException();
            }
    
        private:
            ~VectorSizeToVisibility(){}
        };

    Where, instead of Answer^ you put in your own type.

    If I had to speculate I would say not being able to bind to size is by design.  The issue with allowing binding to IObservableVector->Size is that the binding might have to be re-evaluated every time the Vector contents were modified.  So if you had a loop adding a million items to the vector you would see the binding evaluated a million times.

    Sunday, August 17, 2014 5:50 PM