Implement data bindings using DependencyPropertys

This commit is contained in:
Manuel Thalmann 2024-03-22 02:00:04 +01:00
parent 08f6288f91
commit cad81ce7f1
2 changed files with 17 additions and 5 deletions

View file

@ -5,6 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DT3"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow"
Height="450"
Width="800">
@ -48,8 +49,8 @@
Value="5"/>
</Style>
</StackPanel.Resources>
<TextBlock x:Name="bytesBox" />
<TextBlock x:Name="timeBox" />
<TextBlock Text="{Binding Size, StringFormat={}{0} bytes}" />
<TextBlock Text="{Binding Time, StringFormat={}{0} ms}" />
</StackPanel>
<Grid>
<Grid.RowDefinitions>
@ -64,6 +65,7 @@
<ProgressBar x:Name="timeBar"
Minimum="0"
Maximum="1000"
Value="{Binding Time}"
Grid.ColumnSpan="3" />
<TextBlock Grid.Row="1"
Grid.Column="1"

View file

@ -18,6 +18,16 @@ namespace UrlTester;
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty SizeProperty = DependencyProperty.Register(
"Size",
typeof(int),
typeof(MainWindow));
public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(
"Time",
typeof(long),
typeof(MainWindow));
public MainWindow()
{
InitializeComponent();
@ -36,9 +46,9 @@ public partial class MainWindow : Window
tester.PageLoaded += (_, size) =>
{
stopWatch.Stop();
bytesBox.Text = $"{size} bytes";
timeBox.Text = $"{stopWatch.ElapsedMilliseconds} ms";
timeBar.Value = stopWatch.ElapsedMilliseconds;
SetValue(SizeProperty, size);
SetValue(TimeProperty, stopWatch.ElapsedMilliseconds);
Debug.WriteLine("");
};
_ = tester.GetUrlAsync(urlBox.Text);