2016-04-13 5 views
2

私はWPF/MVVMを使用しています。 私は、textbox.Textをビューモデルのnull可能なdoubleにバインドします。 UpdateSourceTrigger = PropertyChangedであり、Lostfocusではありません。 Double.Parse(textbox.Text)を使用して、使用しているコンバータ内で各桁が入力されると、doubleプロパティが更新されます。他のバリデーションチェックが必要なので、私はPropertyChangedとコンバーターを使用しています。十進数を二重に格納していますか? updatesourcetriggerをPropertyChangedとして使用しているプロパティ

私の問題は「1.69」と入力する必要があることです。 "1"を入力すると、プロパティに "1"が追加されます。 次に、「。」と入力しますが、「1」として追加されません。 double.parseは数字を "1"として保存するので、

10進数を追加することはできません。助けてください。前もって感謝します。

+0

私はこの記事の回答の中のコンバータが必要なものかもしれないと思うhttp://stackoverflow.com/questions/24230085/an-issue-when-textbox-is-binding-to-double-and-enter-負の数である。それは "。" –

答えて

2

StringFormat=\{0:n\}を使用する場合は問題ありません。たとえば:

<TextBox Text="{Binding FooValue, UpdateSourceTrigger=PropertyChanged, 
               StringFormat=\{0:n\}}"/> 

またはコンバータを使用してください。

<Window.Resources>   
    <helper:DoubleConverter x:Key="DoubleConverter" /> 
</Window.Resources> 
...The code omitted for the brevity 
<TextBox Text="{Binding Amount, UpdateSourceTrigger=PropertyChanged,  
         Converter={StaticResource DoubleConverter}}"/> 

DoubleConverter:たとえばあなたが変換する前に、入力した文字列が.で終わる場合

public class DoubleConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     // return an invalid value in case of the value ends with a point 
     return value.ToString().EndsWith(".") ? "." : value; 
    } 
} 
+0

@VishakhBabu質問はお気軽に。私の返事があなたに役立つと感じたら、私の返事を答えとしてマークして、将来の他の人の検索を簡単にすることができます。してください、これを読んでhttp://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – StepUp

1

あなたは、0を追加することができます。

+0

あなたは "1"と言っている。 "10"として追加する必要がありますか? – ViVi

+0

いいえ "1" TextBoxで「1.0」に変更するために「0」を追加します。次に、それを倍に変換してViewModelに格納することができます。しかし、PropertyChanged、bcを呼び出さないようにしてください。それ以外の場合は、ユーザーがまだ入力している間もビュー内で変更されます。 –

+0

そのためには、文字列が "。"で終わるかどうか毎回確認する必要があります。追加し続ける。とにかくチェックします。ありがとうございました。 – ViVi

1

私はちょうどAn issue when TextBox is binding to double and enter negative number that little than -1の私のコメントのコンバータを使用してテストアプリをした:それは私のためにうまく動作します。

私のViewModel:

class MainWindowViewModel 
{ 

    private double? myDouble; 
    public double? MyDouble 
    { 
     get { return myDouble; } 
     set 
     { 
      myDouble = value; 
     } 
    } 
} 

私のテストのメインウィンドウ:予想通り

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525" 
     > 
    <Window.Resources> 
     <local:DecimalConverter x:Key="converter" /> 
    </Window.Resources> 

    <Grid> 
     <TextBox 
      x:Name="textBox" 
      HorizontalAlignment="Left" 
      Text="{Binding MyDouble, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource converter}}" 
      Height="23" 
      Margin="10,185,0,0" 
      TextWrapping="Wrap" 
      VerticalAlignment="Top" 
      Width="120"/> 

     <TextBox 
      x:Name="textBox1" 
      HorizontalAlignment="Left" 
      Text="{Binding MyDouble}" 
      Height="23" 
      Margin="10,248,0,0" 
      TextWrapping="Wrap" 
      VerticalAlignment="Top" 
      Width="120"/> 

    </Grid> 
</Window> 

テキストボックス1は、テキストボックスから更新されます。 Avneeshのコンバータで試してみてください。

+0

上記の答えの完全なアプリ:https://github.com/tim-ashton/WPF-ConverterDemo –

関連する問題