none
How to get correct type conversion for a DependencyProperty on a Custom control. SetValue throws InvalidArgumentException

    Question

  • I'm trying to create a custom control that has a dependencyproperty of type Brush, and it seems that when I set it from XAML with a string (like I can with Brush DPs on builtin classes), I get an InvalidArgumentException from the setter, which claims that I'm passing a value of the wrong type to SetValue, which is true. At the time it breaks for the exception, 'value' is a string, and according to what I've read, SetValue is supposed to be given the correct type. My instinct was to just cast it in the setter, but I've also read that doing anything other than SetValue in that function is verboten.

    For curiosity's sake, I also tried creating a DP of type String, and essentially the same thing happened (value appeared to be an LPCWSTR instead of Platform::String) The exception from this attempt is below:

    First-chance exception at 0x771E2EEC (KernelBase.dll) in Synthesizzler.exe: 0x40080201: WinRT originate error (parameters: 0x80070057, 0x00000002, 0x028EC720).

    First-chance exception at 0x771E2EEC in Synthesizzler.exe: Microsoft C++ exception: Platform::InvalidArgumentException ^ at memory location 0x028ECE04. HRESULT:0x80070057 The parameter is incorrect.

    WinRT information: dp

    This is the source to my control:


    // Header

    //

    // Knob.h

    // Declaration of the Knob class.

    //

    using namespace Windows::UI::Xaml::Controls;

    using namespace Windows::UI::Xaml::Interop;

    using namespace Windows::UI::Xaml;

    using namespace Windows::UI::Xaml::Media;

    using namespace Platform;

    #pragma once

    namespace Synthesizzler

    {

    public ref class Knob sealed : public Windows::UI::Xaml::Controls::Primitives::RangeBase

    {

    public:

    Knob();

    void OnApplyTemplate() override;

    static void RegisterDependencyProperties();

    static property DependencyProperty^ MidgroundProperty

    {

    DependencyProperty^ get() { return _MidgroundProperty; }

    }

    property Brush^ Midground

    {

    // Don't bother putting stuff in here, it's unsupported.

    Brush^ get() { return (Brush^)GetValue(MidgroundProperty); }

    void set(Brush^ value) { SetValue(MidgroundProperty, value); }

    }

    static property DependencyProperty^ LabelProperty

    {

    DependencyProperty^ get() { return _LabelProperty; }

    }

    property String^ Label

    {

    String^ get() { return (String^)GetValue(LabelProperty); }

    void set(String^ value) { SetValue(LabelProperty, value); }

    }

    private:

    //void OnPointerPressed(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args);

    //void OnPointerMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args);

    //Windows::UI::Xaml::Controls::Grid^ m_head; // This is the part that spins

    //Windows::UI::Xaml::Shapes::Ellipse^ m_sensor; // Start spinning when this is touched

    static DependencyProperty^ _MidgroundProperty;

    static DependencyProperty^ _LabelProperty;

    };

    }

     

    // Implementation

    Knob::Knob()

    {

    DefaultStyleKey = "Synthesizzler.Knob";

    }

    DependencyProperty^ Knob::_MidgroundProperty = nullptr;

    DependencyProperty^ Knob::_LabelProperty = nullptr;

    void

    Knob::RegisterDependencyProperties()

    {

    if (nullptr == _MidgroundProperty) {

    _MidgroundProperty = DependencyProperty::Register("Midground", Brush::typeid, Knob::typeid, nullptr);

    }

    if (nullptr == _LabelProperty) {

    _LabelProperty = DependencyProperty::Register("Label", String::typeid, Knob::typeid, nullptr);

    }

    }


    And the XAML snippet in which I try to instantiate one:

    <local:Knobx:Name="KnobWhatever"Grid.Row="1"Grid.Column="1"Label="HERPDERP"Width="50"Height="50"BorderBrush="{StaticResourcestroke}"Foreground="{StaticResourceforeground}"Background="{StaticResourcebackground}"ValueChanged="KnobWhatever_ValueChanged">

    </local:Knob>

    On an unrelated note, does this rich text edit box have a way to reset formatting (Not to mention the bugged-out-state it got in that inserted 5 newlines between the above open and close braces while I was pressing 'delete')? I pasted an exception from VS2013 above, and it seems to have gotten stuck in white-on-white, which made this rather un-fun to write.

    Edit: Copied and pasted into notepad to fix it being impossible to read, and it looks like the formatting got even more messed up, but at least you can read it now. You guys should probably fix this edit box.


    • Edited by MagusOTB Tuesday, December 31, 2013 9:25 PM This richtext control is broken.
    Tuesday, December 31, 2013 12:13 AM

Answers

  • So it turned out I forgot to actually call the RegisterDependencyProperties function. Hazards of copying boilerplate :|
    • Marked as answer by MagusOTB Sunday, January 26, 2014 9:23 PM
    Sunday, January 26, 2014 9:22 PM

All replies

  • Hi MagusOTB,

    I think you'd better edit your post words' color, it is too hard to read your issue.

    You could copy your post to word office tool, and modify the words color, then replace the above words by the words from word.

    Regards!

    Xiaoliang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 31, 2013 9:49 AM
    Moderator
  • Fixed.
    Tuesday, December 31, 2013 10:05 PM
  • Hi MagusOTB,

    Would you mind sharing a sample project that could reproduce your issue with me ?

    It seems that you reference a non-winrt type in a situation that needs a winrt type.

    I'd like to debug your issue and give you further help.

    Regards!

    Xiaoliang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 02, 2014 7:27 AM
    Moderator
  • I've created a minimal project that exhibits the issue, and packaged it as a ZIP. Where should I send/upload it?
    Saturday, January 11, 2014 10:22 PM
  • I uploaded it to my public skydrive at sdrv.ms/1fn7KXe (Also, how do I verify my account to be able to post real links?)
    Sunday, January 12, 2014 1:09 AM
  • Hi MagusOTB,

    For how to verify your account, please post your request here:

    http://social.msdn.microsoft.com/Forums/en-US/200c33c9-abe9-494a-953a-bf53fccb3c76/verify-your-account-11?forum=reportabug

    And I am down loading your sample, I will follow up here as soon as I find the reason.

    Regards!

    Xiaoliang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 13, 2014 5:51 AM
    Moderator
  • So it turned out I forgot to actually call the RegisterDependencyProperties function. Hazards of copying boilerplate :|
    • Marked as answer by MagusOTB Sunday, January 26, 2014 9:23 PM
    Sunday, January 26, 2014 9:22 PM