locked
IMultiValueConverter error crashing visual studio at design time RRS feed

  • Question

  • I am using a MultiValueConverter which is producing an error -> invalid cast

    after the error message I cannot edit the xaml as visual studio crashes. I edit the file in notepad to comment out the binding expression and then I can reload the project. If the comments are removed then visual studio crashes again

    the xaml

     

     

    <local:ADSRThumb x:Name="ADSRThumb1">
           <Canvas.Left>
              <MultiBinding Converter ="{StaticResource timeToPositionConverter}" Mode="OneWay">
                    <Binding ElementName="controlRoot" Path="time1"/>
                    <Binding ElementName="controlRoot" Path="time1Offset"/>                       
              </MultiBinding>
           </Canvas.Left> 
      </local:ADSRThumb>
    
    

     

    i simplified the converter by returning a dummy value, commented out reference to the passed in values in order to try to isolate the problem and avoid any casting error:

     

    class timeToPositionConverter : FrameworkContentElement, IMultiValueConverter
        {
    
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                double xval;
                xval = 10.0;
               // int xOffset = (int)values[0];
               // int timeValue = (int)values[1];          
               //return (double)xOffset + timeValue;
                return xval;
            }
       }
    
    
    stumped

    also: time1 and time1Offset are dependency properties (int) defined on "controlRoot" which is the usercontrol this code exists in

     

     

     


    • Edited by JB90125 Monday, January 16, 2012 5:06 AM more info
    Monday, January 16, 2012 5:03 AM

All replies

  • Hi JB, I hope you're doing fine.

    I believe if the types of values[0] or time1 and values[1] or time1Offset are DateTime, you can't simply convert them to Integer and then you'll have to do the parsing according to your own logic. For example slice the DateTime to the corresponding integer value after converting the DateTime to string.

    I hope this helps.


    Please, mark my reply as Answer if it helps or vote it up if it gives a key to the solution.
    Thanks in advance and good luck.
    Ahmed M. Gamil
    "It is not titles that honor men, but men that honor titles."
    Monday, January 16, 2012 11:45 AM
  • Hi Gamil,

     

    thanks for reply.

     

    values[0] and values[1] are integers in this case - but in any case, if you look at the converter code example posted, any reference to those values is commented out and the converter is simply returning a dummy value which is a double, which is what I believe Canvas.Left expects.

    even if there is a casting error, which is a mystery to me how it could be in this case, I think its odd that Visual Studio crashes and has to restart. it will not even allow me to edit the xaml.

    Monday, January 16, 2012 4:28 PM
  • Anyone get a solution to this?  I am having the same issue.  VS throws "System.InvalidCastException Specified cast is not valid." in the designer, but works fine when I run my project.   This only gives the error in the designer, not when my project runs
    Thursday, July 19, 2012 2:34 PM
  • Please use the as operator rather than cast using (). Change your code like this.

    int? xOffset = values[0] as int?;
    int? timeValue = values[1] as int?;  

    You can take a look at one example here

    http://zamjad.wordpress.com/2011/12/16/one-more-visual-studio-designer-tip/

    Regards

    Zeeshan Amjad

    http://zamjad.wordpress.com/


    http://zamjad.wordpress.com

    Thursday, July 19, 2012 4:37 PM