フォーカスを失った後ではなく、ユーザーが各文字を入力するときに、Windows Phone 7でTextBoxを使用してバインドを更新する方法はありますか?いいえ、結合構文通じWindows Phone 7 TextBoxの "UpdateSourceTrigger = PropertyChanged"
<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/>
フォーカスを失った後ではなく、ユーザーが各文字を入力するときに、Windows Phone 7でTextBoxを使用してバインドを更新する方法はありますか?いいえ、結合構文通じWindows Phone 7 TextBoxの "UpdateSourceTrigger = PropertyChanged"
<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/>
Silverlight for WP7では、指定した構文がサポートされていません。代わりに、次の操作を行います
<TextBox TextChanged="OnTextBoxTextChanged"
Text="{Binding MyText, Mode=TwoWay,
UpdateSourceTrigger=Explicit}" />
UpdateSourceTrigger = Explicit
がここにスマートなボーナスです。 なんですか?Explicit:UpdateSource
メソッドを呼び出すときにのみバインディングソースを更新します。ユーザーが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();
}
解決済み "上記のBindingExpression"コードを使用してバインディングに関連する問題。私のコードを幸せにしてくれてありがとう。 – Jsinh
ないが、それはせずに十分に簡単です:次WPFのTextBoxのように
を行うだろう。 TextChangedイベントを処理し、バインディングでUpdateSourceを呼び出す必要があります。
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
((TextBox) sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
これはかなり簡単にだけでなくattached behaviorに変換することができます。
TextChangedイベントコールUpdateSource()で。
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
。ちょうどあなたがそれらの小さな虫に入っている場合に備えて。
<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();
}
}
}
このテクニックを使用すると、イベントハンドラが決して登録抹消されないため、メモリリークが発生することがあります。 http://sharpfellows.com/post/Memory-Leaks-and-Dependency-Properties.aspx –
@FlatlinerDOA:この場合、参照は非静的オブジェクト(TextBox)から外部オブジェクトへの参照であるため、メモリリークのリスクはありません静的イベントハンドラ(BindingUtility.OnTextChanged)。問題は、参照が逆の場合に発生していました。 –
@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
UpdateSourceTrigger =私のための明示的なdoesntの仕事、それ故イムのTextBox
public class TextBoxEx : TextBox
{
public TextBoxEx()
{
TextChanged += (sender, args) =>
{
var bindingExpression = GetBindingExpression(TextProperty);
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
};
}
}
から誘導体化したカスタムクラスを使用して、コードは1行だけです!
(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
あなたがあなたのページの背後にあるコードでは、一般的なTextChangedイベント(例えば「ImmediateTextBox_TextChanged」)を作成することができ、かつよりは、ページ内の任意のテキストボックスにリンクします。
あなたは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>
私は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}"/>
1:
その後XAMLに、このような呼び出しf最も一般的なWPの問題 –
WP8でまだ見つからない:( – Jedidja