1

フライアウトがあります (以下の関連部分):

<Flyout x:Key="flyoutAverage" FlyoutPresenterStyle="{StaticResource flyoutPresenter}" Closed="Flyout_Closed">
  <Grid>
    <TextBox Grid.Row="1" Grid.Column="0" Text="{Binding One, Mode=TwoWay}" InputScope="Number" Margin="5,0" />
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Two, Mode=TwoWay}" InputScope="Number" Margin="5,0"  />
    <TextBox Grid.Row="1" Grid.Column="2" Text="{Binding Three, Mode=TwoWay}" InputScope="Number" Margin="5,0"  />
    <TextBlock Grid.Row="1" Grid.Column="3" FontSize="18" VerticalAlignment="Center" Text="{Binding Average}" />
 </Grid>
</Flyout>

これは、次のボタンで表示されます。

<StackPanel Orientation="Horizontal" DataContext="{Binding Neck}">
  ...
  <Button Flyout="{StaticResource flyoutAverage}">Enter</Button>
</StackPanel>

ボタン (およびフライアウト) のデータ コンテキストは、このオブジェクトのインスタンスです。

public decimal One
{
  get { return m_One; }
  set { m_One = value; PropChanged("First"); PropChanged("Average"); }
}
public decimal Two
{
  get { return m_Two; }
  set { m_Two = value; PropChanged("Second"); PropChanged("Average"); }
}
public decimal Three
{
  get { return m_Three; }
  set { m_Three = value; PropChanged("Third"); PropChanged("Average"); }
}
public decimal Average
{
  get
  {
    return (One + Two + Three) / 3;
  }
}

意図した動作は、ユーザーがボタンをクリックすると、次のように表示されることです。 スクリーンショット

ユーザーが各テキスト ボックスに値を入力すると、「平均」テキスト ブロックが平均値で自動的に更新されます。

テキストボックスには正しい値が入力されますが、オブジェクトにプッシュバックされることはありません。フライアウトを閉じて再度開くと、値が保持されます。TwoWay として明確に設定されている場合、Binding モードが OneWay であるかのようです。

何か案は?それとも、私はそれを間違っていますか?

4

3 に答える 3

3

バインディングを行っているときに、データ変換を処理せずに adecimalから aに直接バインドすることはできません。プロパティは文字列型です。このような単純なコンバーターが必要になります ( ( reference ) を使用):TextBoxTwoWayTextBox TextIValueConverter

public class DecimalConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
                          object parameter, string language)
    {
        return value.ToString();
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, string language)
    {
        decimal d;
        if (value is string)
        {
            if (decimal.TryParse((string)value, out d))
            {
                return d;
            }
        }
        return 0.0;
    }
}

そしてとしてResource

<local:DecimalConverter x:Key="decimalConverter" />

次に、使用中:

<TextBox Grid.Row="1" Grid.Column="0" 
        Text="{Binding One, Mode=TwoWay, 
                 Converter={StaticResource decimalConverter}}" 
        InputScope="Number" Margin="5,0" />

InputScopejust は、フィールドがフォーカスを取得したときにデフォルトで表示されるキーボードに関する Windows への提案です。不要なキー (または英字) は防止されません。

PropChangedまた、必ず変更するプロパティの名前で呼び出してください。あなたのコードでは間違っています。

XAML/WPF アプリケーションでバインドが期待どおりに機能しない場合に常に使用する必要があることの 1 つは、Visual StudioOutputウィンドウでエラーを確認することです。たとえば、次のように表示されます。

エラー: 値をターゲットからソースに保存できません。BindingExpression: Path='Two' DataItem='Win8CSharpTest.NeckData, Win8CSharpTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; ターゲット要素は 'Windows.UI.Xaml.Controls.TextBox' (Name='null') です。ターゲット プロパティは 'Text' (タイプ 'String') です。

于 2014-02-08T16:31:35.387 に答える
2

誰かが将来この問題に直面したかどうかを共有するために、私は上記のコンバーターを使用しましたが、変換が機能しない理由について頭を悩ませていました. たまたま、私のロケールの小数点記号は「.」ではなく「,」でした。したがって、これに対処するために、コンバーターを次のように変更しました。


    public class DecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, string language)
        {
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType,
                                  object parameter, string language)
        {
            if (value is string)
            {
                decimal d;
                var formatinfo = new NumberFormatInfo();

                formatinfo.NumberDecimalSeparator = ".";

                if (decimal.TryParse((string)value, NumberStyles.Float, formatinfo, out d))
                {
                    return d;
                }

                formatinfo.NumberDecimalSeparator = ",";

                if (decimal.TryParse((string)value, NumberStyles.Float, formatinfo, out d))
                {
                    return d;
                }
            }
            return 0.0;
        }
    }

于 2014-05-30T08:26:48.337 に答える