本帖最后由 porenasckx 于 2014-12-02 17:59:27 编辑

解决方案 »

  1.   

    其实,都没有问题,或者说问题不是存在于依赖项属性。<TextBox Width="200" Height="28" Text="{Binding TestNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    这个绑定中,有没有UpdateSourceTrigger,现象是完全不一样的。
    原因就在于:
    1. 如果没有UpdateSourceTrigger,输什么都可以输,包括特殊字符,在此控件失去焦点的时候,.Net会去进行数据验证,如果发现输入不是有效的数据,比如上面的控件,就会去验证是不是有效的double型数据。如果不是,控件周围会出现红色框框,表示输入有误;
    如果有UpdateSourceTrigger,那就不再是失去焦点再进行数据验证了,而是每输一个字符就验证一下,即TextChanged就会验证。
    那么,“0.“转换为有效的double型数据,自然就是0,一输入小数点,就被转换转没了。所以你认为没输入。     具体解决方案,那就得自定义控件了。必要时,还得自定义数据验证方式。
      

  2.   


       <Button x:Name="Btn" Content="Button" HorizontalAlignment="Left" Margin="412,341,0,0" VerticalAlignment="Top" Width="75"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="397,416,0,0" TextWrapping="Wrap" Text="{Binding ElementName=Btn,Path=Width,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    我刚试了,在vs2010没问题,但是到2012就不行了?
      

  3.   

       <Button x:Name="Btn" Content="Button" HorizontalAlignment="Left" Margin="412,341,0,0" VerticalAlignment="Top" Width="75"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="397,416,0,0" TextWrapping="Wrap" Text="{Binding ElementName=Btn,Path=Width,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
    我刚试了,在vs2010没问题,但是到2012就不行了?
    5楼说的有道理。于是我就查看了width的”从元数据“信息,发现其值必须大于0.0,任何转变不成double的都将视为System.Double.NaN类型。
      

  4.   

    恩,也就是说,诸如Width=“*”这种情况,宽度是没有值的,即值是System.Double.NaN。
      

  5.   


    那自定义的double依赖属性怎么在运行后输入值,难道只能输入整数
      

  6.   

    TextBox .Text 属性的默认 UpdateSourceTrigger 值为 LostFocus。 这意味着如果应用程序的 TextBox 包含数据绑定 TextBox.Text 属性,则直到 TextBox 失去焦点(例如,将鼠标移到 TextBox 外单击时),键入到 TextBox 中的文本才能更新源。你为啥要修改为PropertyChanged呢?
      

  7.   

    改为UpdateSourceTrigger=PropertyChanged
    没有任何意义,反而会在输入过程中频繁的去更改属性值