Skip to content

Commit

Permalink
Update IP address on both PropertyChanged and LostFocus
Browse files Browse the repository at this point in the history
  • Loading branch information
astenlund committed Oct 19, 2020
1 parent 06772cf commit f1ba693
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
32 changes: 32 additions & 0 deletions Behaviors/UpdateSourceOnLostFocusBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Xaml.Behaviors;

namespace fs2ff.Behaviors
{
public class UpdateSourceOnLostFocusBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();

if (AssociatedObject != null)
AssociatedObject.LostFocus += AssociatedObject_LostFocus;
}

protected override void OnDetaching()
{
if (AssociatedObject != null)
AssociatedObject.LostFocus -= AssociatedObject_LostFocus;

base.OnDetaching();
}

private static void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
{
if (sender is TextBox textBox)
BindingOperations.GetBindingExpression(textBox, TextBox.TextProperty)?.UpdateSource();
}
}
}
7 changes: 4 additions & 3 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behaviors="clr-namespace:fs2ff.Behaviors"
xmlns:b="clr-namespace:fs2ff.Behaviors"
xmlns:c="clr-namespace:fs2ff.Converters"
ui:WindowHelper.UseModernWindowStyle="True"
mc:Ignorable="d"
Expand Down Expand Up @@ -86,12 +86,13 @@
Text="{
Binding IpAddress, Mode=TwoWay,
Converter={c:IpAddressToStringConverter},
UpdateSourceTrigger=LostFocus}"
UpdateSourceTrigger=PropertyChanged, Delay=5000}"
Template="{StaticResource WatermarkTextBoxTemplate}"
Tag="255.255.255.255"
Margin="0,10,0,0">
<i:Interaction.Behaviors>
<behaviors:MoveFocusOnEnterBehavior />
<b:MoveFocusOnEnterBehavior />
<b:UpdateSourceOnLostFocusBehavior />
</i:Interaction.Behaviors>
</TextBox>
<Label
Expand Down

0 comments on commit f1ba693

Please sign in to comment.