locked
Cannot access custom control in code

    Question

  • I've created a custom numeric up down control in c++. It has 3 custom properties, here's one

    //header
    private:
    	static DependencyProperty^ _valueProperty;
    public:
    
    		static property DependencyProperty^ ValueProperty
    		{
    			DependencyProperty^ get() { return _valueProperty; }
    		}
    
    		property int Value
    		{
    			int get() { return (int)GetValue(ValueProperty); }
    			void set(int value) 
    			{ 
    				SetValue(ValueProperty, value);
    				tbNum->Text = value.ToString();
    				if(value != sldNum->Value)
    				{
    					if(value >= MinValue)
    						sldNum->Value = value;
    					else 
    						sldNum->Value = MinValue;
    				}
    			}
    		}
    
    //source
    static void OnValueChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e)
    {
     NumericUpDown^ tb = (NumericUpDown^)d;
       int val = static_cast<int>(e->NewValue);
       if(tb != nullptr)
            tb->Value = val;
    }
    
    DependencyProperty^ NumericUpDown::_valueProperty = DependencyProperty::Register("Value", int::typeid , NumericUpDown::typeid, 
    														 ref new PropertyMetadata(10, ref new PropertyChangedCallback(&OnValueChanged))); 
    

    there is a slider control and a textbox, works great, binds great looks like this in the xaml

    <local:NumericUpDown x:Name="nudMain" Value="{Binding SelectedCount, Mode=TwoWay}" MaxValue="{Binding AvailCnt, Mode=TwoWay}" MinValue="{Binding MinValue, Mode=TwoWay}"
                         Margin="0,10,0,0" Visibility="{Binding NumVisibile}" FontFamily="Global User Interface" Loaded="nudMain_Loaded"/>

    When I try to access it from code behind it shows up in the IntelliSense but it shows no properties methods etc from the -> and I get errors on compile.

    Error 1 error C2027: use of undefined type 'FLCpp::NumericUpDown' c:\users\bubbarichard\documents\visual studio 2012\projects\'FLCpp\'FLCpp\pages\gamesoptionspage.xaml.cpp 325 1 'FLCpp
    Error 2 error C2227: left of '->Value' must point to class/struct/union/generic type c:\users\bubbarichard\documents\visual studio 2012\projects\'FLCpp\'FLCpp\pages\gamesoptionspage.xaml.cpp 325 1 'FLCpp

    Why? Ideas? suggestions? etc.....

    Thanks,

    Bubba

    Tuesday, July 16, 2013 12:29 PM

Answers

  • sldNum is defined in the custom control, you asked for where the error was occurring. sldNum is defined in an auto generated file since it is declared in the xaml.

    The problem was solved by adding the control from the tool box which auto added a reference to the custom controls header file. The custom control can be added to the xaml and bound to data without adding the reference to the header file but once you want to access it from the c++ code the header must be referenced.

    Thanks!

    • Marked as answer by BubbaRichard Tuesday, July 16, 2013 11:00 PM
    Tuesday, July 16, 2013 11:00 PM

All replies

  • Could you post your header file or at least the definition of class that the compile error is occurring?
    Tuesday, July 16, 2013 8:06 PM
  • // // OptionsPage.xaml.h // Declaration of the OptionsPage class // #pragma once #include "Pages\BasePage.xaml.h" #include "Pages\OptionsPage.g.h" #include "Controls\NumericUpDown.xaml.h" namespace FLCpp { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> [Windows::Foundation::Metadata::WebHostHidden] public ref class OptionsPage sealed { public: OptionsPage(); protected: virtual void LoadState(Platform::Object^ navigationParameter, Windows::Foundation::Collections::IMap<Platform::String^, Platform::Object^>^ pageState) override; virtual void SaveState(Windows::Foundation::Collections::IMap<Platform::String^, Platform::Object^>^ pageState) override; virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override; virtual void OnNavigatedFrom(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override; private: Platform::String^ _currentKey, ^_dictFile; Platform::Collections::Vector<Platform::String^>^ _selectedCats; void go_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void CheckBox_Checked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void CheckBox_Unchecked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void itemListView_ItemClick(Platform::Object^ sender, Windows::UI::Xaml::Controls::ItemClickEventArgs^ e); }; }


    Thought that was enough, here it is, got it to work by inserting the control from the toolbox but would still like to know what was happening.

    Tuesday, July 16, 2013 9:04 PM
  • How is sldNum defined? I don't see where it is declared. Obviously the compiler thinks it is a different type then what you expect it to be.

    Tuesday, July 16, 2013 10:32 PM
  • sldNum is defined in the custom control, you asked for where the error was occurring. sldNum is defined in an auto generated file since it is declared in the xaml.

    The problem was solved by adding the control from the tool box which auto added a reference to the custom controls header file. The custom control can be added to the xaml and bound to data without adding the reference to the header file but once you want to access it from the c++ code the header must be referenced.

    Thanks!

    • Marked as answer by BubbaRichard Tuesday, July 16, 2013 11:00 PM
    Tuesday, July 16, 2013 11:00 PM