È possibile creare un approccio XAML puro creando uno attached behaviour.
Qualcosa di simile a questo:
public static class InputBindingsManager
{
public static readonly DependencyProperty UpdatePropertySourceWhenEnterPressedProperty = DependencyProperty.RegisterAttached(
"UpdatePropertySourceWhenEnterPressed", typeof(DependencyProperty), typeof(InputBindingsManager), new PropertyMetadata(null, OnUpdatePropertySourceWhenEnterPressedPropertyChanged));
static InputBindingsManager()
{
}
public static void SetUpdatePropertySourceWhenEnterPressed(DependencyObject dp, DependencyProperty value)
{
dp.SetValue(UpdatePropertySourceWhenEnterPressedProperty, value);
}
public static DependencyProperty GetUpdatePropertySourceWhenEnterPressed(DependencyObject dp)
{
return (DependencyProperty)dp.GetValue(UpdatePropertySourceWhenEnterPressedProperty);
}
private static void OnUpdatePropertySourceWhenEnterPressedPropertyChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
UIElement element = dp as UIElement;
if (element == null)
{
return;
}
if (e.OldValue != null)
{
element.PreviewKeyDown -= HandlePreviewKeyDown;
}
if (e.NewValue != null)
{
element.PreviewKeyDown += new KeyEventHandler(HandlePreviewKeyDown);
}
}
static void HandlePreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
DoUpdateSource(e.Source);
}
}
static void DoUpdateSource(object source)
{
DependencyProperty property =
GetUpdatePropertySourceWhenEnterPressed(source as DependencyObject);
if (property == null)
{
return;
}
UIElement elt = source as UIElement;
if (elt == null)
{
return;
}
BindingExpression binding = BindingOperations.GetBindingExpression(elt, property);
if (binding != null)
{
binding.UpdateSource();
}
}
}
Poi, nel tuo XAML si imposta la proprietà InputBindingsManager.UpdatePropertySourceWhenEnterPressedProperty
per quello che si desidera aggiornare quando il Enter viene premuto chiave. Ti piace questa
<TextBox Name="itemNameTextBox"
Text="{Binding Path=ItemName, UpdateSourceTrigger=PropertyChanged}"
b:InputBindingsManager.UpdatePropertySourceWhenEnterPressed="TextBox.Text"/>
(Hai solo bisogno di assicurarsi di includere un riferimento CLR-namespace xmlns per "b" nell'elemento radice del file XAML indicando che mai spazio dei nomi si inserisce l'InputBindingsManager in).
fonte
2009-02-19 10:26:15
ho provato ma sembra webbrowser non aggiorna la sua fonte. qualsiasi cosa mi sia sfuggita? non ho gestito nessun evento presumo legando 2 way, il suo evento di gestione automatica. ho sbagliato? Grazie. –
Nel tentativo di salvare i lettori del futuro alcuni minuti di giocherellare, l'ho usato nel mio progetto e l'esempio XAML sopra riportato non funzionava correttamente. La fonte è stata aggiornata ad ogni cambio di personaggio. La modifica di "UpdateSourceTrigger = PropertyChanged" su "UpdateSourceTrigger = Explicit" ha risolto il problema. Ora funziona tutto come desiderato. – ihake
@ihake: Penso che la modifica consigliata impedirà anche l'aggiornamento sulla messa a fuoco persa anche se – VoteCoffee