Answered WinRT XAML : Binding text to decimal causes

  • giovedì 10 maggio 2012 18:15
     
      Contiene codice

    When binding a textbox's Text property to a decimal on a view model using two way binding, I'm receiving the exception below.  The exception occurs when leaving the text box.  It works fine when using a float.

    Object of type 'System.String' cannot be converted to type 'System.Decimal'.

    Value does not fall within the expected range.
    <Page
        x:Class="DecimalBindingTest.BlankPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:DecimalBindingTest"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Page.DataContext>
            <local:ViewModel />
        </Page.DataContext>
        
        <StackPanel>
            <TextBox Text="{Binding Decimal, Mode=TwoWay}" />
         </StackPanel>
    </Page>

    public class ViewModel { private decimal m_Decimal; public decimal Decimal { get { return m_Decimal; } set { m_Decimal = value; } } }


Tutte le risposte

  • venerdì 11 maggio 2012 19:04
    Moderatore
     
     Con risposta
    This is by design.  There's a long and complicated answer for this which I am not going into.  Please use float.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator If my reply answers your question, please mark this post as answered.

  • sabato 12 maggio 2012 21:08
     
     
    Hey Matt: Actually I'd like to understand this limitation better. Is there a reference you can point me to why this is?
  • lunedì 21 maggio 2012 12:59
     
     
    It would be nice to know, but he marked his previous answer as the official answer.
  • domenica 12 agosto 2012 09:35
     
     

    Had the same problem, and found this Forum post.  Agree, would be nice to understand why this behaviour.  Not had this same issue with WP7 (at least with other controls), so not clear why it shouldn't be supported. 

    Can we at least get a link to the long and complicated answer somewhere else?

    Thanks.

  • sabato 15 dicembre 2012 21:08
     
     
    Too bad we can't vote as Unhelpful

    Luc Morin, T.P. http://www.stlm.ca

  • domenica 16 dicembre 2012 01:23
     
     

    In case some other poor soul runs into this question, there is a simple solution.  Creating a IValueConverter solves the problem as described by Stefan here http://social.msdn.microsoft.com/Forums/is/winappswithcsharp/thread/b6099324-e66c-42ef-9bfe-a0f022e2b6ab.



    Darrel