2011-01-28 10 views
34

フォーカスを失った後ではなく、ユーザーが各文字を入力するときに、Windows Phone 7でTextBoxを使用してバインドを更新する方法はありますか?いいえ、結合構文通じWindows Phone 7 TextBoxの "UpdateSourceTrigger = PropertyChanged"

<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/> 
+1

1:

Public Class TextBoxUpdate : Inherits TextBox Private Sub OnTextBoxTextChanged(sender As Object, e As TextChangedEventArgs) Handles Me.TextChanged Dim senderText As TextBox = DirectCast(sender, TextBox) Dim bindingExp As BindingExpression = senderText.GetBindingExpression(TextBox.TextProperty) bindingExp.UpdateSource() End Sub End Class 

その後XAMLに、このような呼び出しf最も一般的なWPの問題 –

+0

WP8でまだ見つからない:( – Jedidja

答えて

51

Silverlight for WP7では、指定した構文がサポートされていません。代わりに、次の操作を行います

<TextBox TextChanged="OnTextBoxTextChanged" 
     Text="{Binding MyText, Mode=TwoWay, 
       UpdateSourceTrigger=Explicit}" /> 

UpdateSourceTrigger = Explicitがここにスマートなボーナスです。 なんですか?ExplicitUpdateSourceメソッドを呼び出すときにのみバインディングソースを更新します。ユーザーがTextBoxを離れると、余分なバインディングセットが1つ保存されます。 C#の

:私は添付プロパティを使用して好き

private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e) 
{ 
    TextBox textBox = sender as TextBox; 
    // Update the binding source 
    BindingExpression bindingExpr = textBox.GetBindingExpression(TextBox.TextProperty); 
    bindingExpr.UpdateSource(); 
} 
+0

解決済み "上記のBindingExpression"コードを使用してバインディングに関連する問題。私のコードを幸せにしてくれてありがとう。 – Jsinh

5

ないが、それはせずに十分に簡単です:次WPFのTextBoxのように

を行うだろう。 TextChangedイベントを処理し、バインディングでUpdateSourceを呼び出す必要があります。

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    ((TextBox) sender).GetBindingExpression(TextBox.TextProperty).UpdateSource(); 
} 

これはかなり簡単にだけでなくattached behaviorに変換することができます。

1

TextChangedイベントコールUpdateSource()で。

BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty); 
be.UpdateSource(); 
23

。ちょうどあなたがそれらの小さな虫に入っている場合に備えて。

<toolkit:DataField Label="Name"> 
    <TextBox Text="{Binding Product.Name, Mode=TwoWay}" c:BindingUtility.UpdateSourceOnChange="True"/> 
</toolkit:DataField> 

そして、バッキングコード。

public class BindingUtility 
{ 
public static bool GetUpdateSourceOnChange(DependencyObject d) 
{ 
    return (bool)d.GetValue(UpdateSourceOnChangeProperty); 
} 

public static void SetUpdateSourceOnChange(DependencyObject d, bool value) 
{ 
    d.SetValue(UpdateSourceOnChangeProperty, value); 
} 

// Using a DependencyProperty as the backing store for … 
public static readonly DependencyProperty 
    UpdateSourceOnChangeProperty = 
    DependencyProperty.RegisterAttached(
    "UpdateSourceOnChange", 
    typeof(bool), 
       typeof(BindingUtility), 
    new PropertyMetadata(false, OnPropertyChanged)); 

private static void OnPropertyChanged (DependencyObject d, 
    DependencyPropertyChangedEventArgs e) 
{ 
    var textBox = d as TextBox; 
    if (textBox == null) 
    return; 
    if ((bool)e.NewValue) 
    { 
    textBox.TextChanged += OnTextChanged; 
    } 
    else 
    { 
    textBox.TextChanged -= OnTextChanged; 
    } 
} 
static void OnTextChanged(object s, TextChangedEventArgs e) 
{ 
    var textBox = s as TextBox; 
    if (textBox == null) 
    return; 

    var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty); 
    if (bindingExpression != null) 
    { 
    bindingExpression.UpdateSource(); 
    } 
} 
} 
+1

このテクニックを使用すると、イベントハンドラが決して登録抹消されないため、メモリリークが発生することがあります。 http://sharpfellows.com/post/Memory-Leaks-and-Dependency-Properties.aspx –

+0

@FlatlinerDOA:この場合、参照は非静的オブジェクト(TextBox)から外部オブジェクトへの参照であるため、メモリリークのリスクはありません静的イベントハンドラ(BindingUtility.OnTextChanged)。問題は、参照が逆の場合に発生していました。 –

+0

@Parrhesia Joe、ほとんどのツールキットのテキストボックスに振る舞いを添付しています。テキストボックスに 'Toolkit:PhoneTextBox Hint =" Last 10 Digit Card Number "を使用しました。 \t \t \t \t Grid.Row =" 0 " InputScope = "番号" のMaxLength = "10" \t \t \t \t DisplayedMaxLength = "10" テキスト= "{カード番号結合、モード=双方向}" ActionIcon = "/資産/ Help.png" ヘルパー:BindingUtility .UpdateSourceOnChange = "True" KeyDown = "PhoneTextBox_KeyDown" />これにはいくつかの問題がありますが、0を押すとカーソルが最初の項目に移動するようです – Eldho

0

UpdateSourceTrigger =私のための明示的なdoesntの仕事、それ故イムのTextBox

public class TextBoxEx : TextBox 
{ 
    public TextBoxEx() 
    { 
     TextChanged += (sender, args) => 
          { 
           var bindingExpression = GetBindingExpression(TextProperty); 
           if (bindingExpression != null) 
           { 
            bindingExpression.UpdateSource(); 
           } 
          }; 
    } 

} 
0

から誘導体化したカスタムクラスを使用して、コードは1行だけです!

(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource(); 

あなたがあなたのページの背後にあるコードでは、一般的なTextChangedイベント(例えば「ImmediateTextBox_TextChanged」)を作成することができ、かつよりは、ページ内の任意のテキストボックスにリンクします。

1

あなたはTextChangedの更新を処理するために、独自のTextBoxの動作を記述することができます。

これはPasswordBoxに私のサンプルですが、あなたはそれが任意のオブジェクトの任意のプロパティを処理するために簡単な変更をすることができます。

public class UpdateSourceOnPasswordChangedBehavior 
    : Behavior<PasswordBox> 
{ 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     AssociatedObject.PasswordChanged += OnPasswordChanged; 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 
     AssociatedObject.PasswordChanged -= OnPasswordChanged; 
    } 

    private void OnPasswordChanged(object sender, RoutedEventArgs e) 
    { 
     AssociatedObject.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource(); 
    } 
} 

Ussage:

<PasswordBox x:Name="Password" Password="{Binding Password, Mode=TwoWay}" > 
    <i:Interaction.Behaviors> 
     <common:UpdateSourceOnPasswordChangedBehavior/> 
    </i:Interaction.Behaviors> 
</PasswordBox> 
0

私はPraetorian's answerを取ったので、あなたは、この動作での背後にあるビューのコードを混乱する必要はありませんTextBoxを継承する拡張クラスを作りました。

C-シャープ:

public class TextBoxUpdate : TextBox 
{ 
    public TextBoxUpdate() 
    { 
     TextChanged += OnTextBoxTextChanged; 
    } 
    private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e) 
    { 
     TextBox senderText = (TextBox)sender; 
     BindingExpression bindingExp = senderText.GetBindingExpression(TextBox.TextProperty); 
     bindingExp.UpdateSource(); 
    } 
} 

VisualBasicの:1のOを尋ねるため

<local:TextBoxUpdate Text="{Binding PersonName, Mode=TwoWay}"/> 
関連する問題