diff --git a/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml b/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml index b6b7d65..b6edf53 100644 --- a/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml +++ b/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml @@ -4,14 +4,32 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:pages="clr-namespace:Ursa.Demo.Pages" xmlns:u="https://irihi.tech/ursa" d:DesignHeight="450" d:DesignWidth="800" mc:Ignorable="d"> + + + - - + + + + + + + diff --git a/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml.cs b/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml.cs index 4c7d175..c865da8 100644 --- a/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/IPv4BoxDemo.axaml.cs @@ -1,6 +1,9 @@ +using System; +using System.Net; using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; +using CommunityToolkit.Mvvm.ComponentModel; namespace Ursa.Demo.Pages; @@ -9,10 +12,18 @@ public partial class IPv4BoxDemo : UserControl public IPv4BoxDemo() { InitializeComponent(); + DataContext = new IPv4DemoViewMode(); } +} - private void InitializeComponent() +public partial class IPv4DemoViewMode: ObservableObject +{ + [ObservableProperty] + private IPAddress? _address; + + public void ChangeAddress() { - AvaloniaXamlLoader.Load(this); + long l = Random.Shared.NextInt64(0x00000000FFFFFFFF); + Address = new IPAddress(l); } } \ No newline at end of file diff --git a/src/Ursa/Controls/IPv4Box.cs b/src/Ursa/Controls/IPv4Box.cs index 6c6c649..30bcbf5 100644 --- a/src/Ursa/Controls/IPv4Box.cs +++ b/src/Ursa/Controls/IPv4Box.cs @@ -90,6 +90,7 @@ public class IPv4Box: TemplatedControl static IPv4Box() { ShowLeadingZeroProperty.Changed.AddClassHandler((o, e) => o.OnFormatChange(e)); + IPAddressProperty.Changed.AddClassHandler((o, e) => o.OnIPChanged(e)); } private void OnFormatChange(AvaloniaPropertyChangedEventArgs arg) @@ -98,6 +99,32 @@ public class IPv4Box: TemplatedControl ParseBytes(showLeadingZero); } + private void OnIPChanged(AvaloniaPropertyChangedEventArgs arg) + { + IPAddress? address = arg.GetNewValue(); + if (address is null) + { + foreach (var presenter in _presenters) + { + if(presenter!=null) presenter.Text = string.Empty; + } + ParseBytes(ShowLeadingZero); + } + else + { + var sections = address.ToString().Split('.'); + for (int i = 0; i < 4; i++) + { + var presenter = _presenters[i]; + if (presenter != null) + { + presenter.Text = sections[i]; + } + } + ParseBytes(ShowLeadingZero); + } + } + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e);