有沒有辦法讓Windows Phone 7中的TextBox更新綁定,因爲用戶鍵入每個字母而不是失去焦點後?「UpdateSourceTrigger = PropertyChanged」相當於Windows Phone 7 TextBox
像下面的文本框WPF會做:
<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/>
有沒有辦法讓Windows Phone 7中的TextBox更新綁定,因爲用戶鍵入每個字母而不是失去焦點後?「UpdateSourceTrigger = PropertyChanged」相當於Windows Phone 7 TextBox
像下面的文本框WPF會做:
<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
時,它可以爲您節省一個額外的綁定設置。
在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 2013-01-11 08:13:28
不通過綁定語法,沒有,但它是不很容易。您必須處理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();
}
}
}
UpdateSourceTrigger =我明確不工作,因此進出口使用自TextBox
public class TextBoxEx : TextBox
{
public TextBoxEx()
{
TextChanged += (sender, args) =>
{
var bindingExpression = GetBindingExpression(TextProperty);
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
};
}
}
衍生化自定義類它的代碼只是一條線!
(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();
您可以在頁面的後臺代碼創建一個通用的TextChanged事件(例如「ImmediateTextBox_TextChanged」),並且比它在頁面鏈接到任何文本框。
您可以編寫自己的文本框的行爲來處理框TextChanged更新:
這是我的樣品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:
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這樣調用:
<local:TextBoxUpdate Text="{Binding PersonName, Mode=TwoWay}"/>
+1詢問一個O f最常見的WP問題 – 2012-03-19 17:49:19
並且仍然在WP8中丟失:( – Jedidja 2013-12-11 16:39:13