Silverlight DatagridでのUpdateSourceTrigger LostFocusの回避策は?
-
05-07-2019 - |
質問
OnTabSelectionChangedデータを検証するSilverlight 2アプリケーションがあります。コントロールからタブ移動せずにタブをクリックすると、検証前にLINQオブジェクトが更新されないため、すぐにUpdateSourceTriggerがLostFocus以上のものを許可することを望み始めました。
フォーカスを別のコントロールに設定してからOnTextChangedに戻すことで、TextBoxの問題を回避しました:
Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
txtSetFocus.Focus()
sender.Focus()
End Sub
今、DataGrid内で同じ種類のハックを達成しようとしています。 My DataGridは、実行時にCellTemplateおよびCellEditingTemplateに対して生成されたDataTemplatesを使用します。 TextChanged =" OnTextChanged"を書いてみました。 DataTemplateのTextBoxに入力しますが、トリガーされません。
誰にもアイデアはありますか?
解決
<のhref = "http://blog.mustoverride.com/2010/01/silverlight-updatesourcetrigger.html?showComment=1279149377285_AIe9_BFLaR4pbpg_swaitgIUU0PR-hQVluTHi6P3siH156dRQIYsaNKVxx_ptreNpwv-HaozS_tab7wC55uFRxgzpOU22HtkC6-Cz2DwtrHdnmZZ5dn0sOwczJ_MpPg5K5LsT24F6GdNHZyI9LYI4kQQaU0V7rtwuIkU1AsYxATywIYpt_ZIRS5lh4WjahPQqu4-c9TBPqHvJN0zQAQGAUsCeuIa0h_HR-rE7iUy4ajM3zzOACX3B9D3tQBXhLrGf0q0mG67thiUcZZqgvzr2eNENemhQpWLX_5Kky7Nj2WDwQcOxIDsYlWz6WTjVu8_UPVFuSTVJjmGajc9rEGDX7Xcq1ZwzVXhRTMCTw_IGXWYm_pp0in0V5G5LEwKViKkNLgFWL3BazgXZQjtFsnFT4Y2-K5FxMGIGmufAAo5uwMM1Q3GLbXwlhwAAcxz3Gar6Z-olH7ylmobDKmCKNedO2zb0ctdkPQZe7U2yYRg5OY-cI3PQtygpkQpjG6ap9yvUIf6Gpd6HErk8Q6OFy4AlpXqiiIriABLE-E1b- e9w44l1T_FQhGLEtiJD4bvY2VZXx5Pe1_0EISdX_PNPcN1E3a5iOmq7CppTkmY64gar1XZtWZ7xdZ18WldNXG3LDFEInWlUiSngpJdBqv0wT81_-RwxJuGY8Faq2iOATzih4274u1bJa73-HWzcepR9G5nl33NBWNCOWZbuW_Q8heTU6ea02X3D-E767OCxHTBi_NB7pRDyAq_stQiFPTD3H - G-DWrhVaI44PXBia88huy4-b1PIEyUzatDm747Hyw9ilRsQPH9RgUbuKocnTBbssmuhq8hK4vErFspeh-nEIHLVvyTxNs-YWhHKBpgY27k5bLZU1fSdyudwの# c8238632009983091264 "rel =" noreferrer ">テキストボックスにもビヘイビアを適用してそれを行うことができます
// xmlns:int is System.Windows.Interactivity from System.Windows.Interactivity.DLL)
// xmlns:behavior is your namespace for the class below
<TextBox Text="{Binding Description,Mode=TwoWay,UpdateSourceTrigger=Explicit}">
<int:Interaction.Behaviors>
<behavior:TextBoxUpdatesTextBindingOnPropertyChanged />
</int:Interaction.Behaviors>
</TextBox>
public class TextBoxUpdatesTextBindingOnPropertyChanged : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.TextChanged -= TextBox_TextChanged;
}
void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var bindingExpression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
bindingExpression.UpdateSource();
}
}
他のヒント
このブログ投稿では、添付プロパティを使用して明示的にテキストボックスのソースを更新する方法を示しています。 http ://www.thomasclaudiushuber.com/blog/2009/07/17/here-it-is-the-updatesourcetrigger-for-propertychanged-in-silverlight/
他のコントロールでも動作するように簡単に変更できます...
MVVMとSilverlight 4を使用してこの同じ問題に遭遇しました。問題は、テキストボックスがフォーカスを失うまでバインディングがソースを更新しないが、別のコントロールにフォーカスを設定してもうまくいかないことです。
2つの異なるブログ投稿の組み合わせを使用した解決策を見つけました。 Patrick CauldwellのDefaultButtonHubコンセプトのコードを使用し、「SmallWorkaround」を1つ使用しました。 SmallWorkarounds.netから
http://www.cauldwell.net/patrick/blog/DefaultButtonSemanticsInSilverlightRevisited.aspx
www.smallworkarounds.net/2010/02/elementbindingbinding-modes.html
私の変更により、DefaultButtonHubクラスの次のコードが作成されました。
public class DefaultButtonHub
{
ButtonAutomationPeer peer = null;
private void Attach(DependencyObject source)
{
if (source is Button)
{
peer = new ButtonAutomationPeer(source as Button);
}
else if (source is TextBox)
{
TextBox tb = source as TextBox;
tb.KeyUp += OnKeyUp;
}
else if (source is PasswordBox)
{
PasswordBox pb = source as PasswordBox;
pb.KeyUp += OnKeyUp;
}
}
private void OnKeyUp(object sender, KeyEventArgs arg)
{
if (arg.Key == Key.Enter)
if (peer != null)
{
if (sender is TextBox)
{
TextBox t = (TextBox)sender;
BindingExpression expression = t.GetBindingExpression(TextBox.TextProperty);
expression.UpdateSource();
}
((IInvokeProvider)peer).Invoke();
}
}
public static DefaultButtonHub GetDefaultHub(DependencyObject obj)
{
return (DefaultButtonHub)obj.GetValue(DefaultHubProperty);
}
public static void SetDefaultHub(DependencyObject obj, DefaultButtonHub value)
{
obj.SetValue(DefaultHubProperty, value);
}
// Using a DependencyProperty as the backing store for DefaultHub. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DefaultHubProperty =
DependencyProperty.RegisterAttached("DefaultHub", typeof(DefaultButtonHub), typeof(DefaultButtonHub), new PropertyMetadata(OnHubAttach));
private static void OnHubAttach(DependencyObject source, DependencyPropertyChangedEventArgs prop)
{
DefaultButtonHub hub = prop.NewValue as DefaultButtonHub;
hub.Attach(source);
}
}
これは、Silverlightの何らかのドキュメントに含める必要があります:)
古いニュースであることは知っていますが...
Text =&quot; {Binding Path = newQuantity、UpdateSourceTrigger = PropertyChanged}&quot;