Skip to content

Commit f10e4e3

Browse files
Copilotayushjai19
andcommitted
Simplify button state logic using x:Bind function binding
Co-authored-by: ayushjai19 <244442986+ayushjai19@users.noreply.github.com>
1 parent cac0ba8 commit f10e4e3

File tree

2 files changed

+4
-38
lines changed

2 files changed

+4
-38
lines changed

WinUIGallery/Samples/ControlPages/Fundamentals/Controls/TemperatureConverterControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<Button Content="Convert to Fahrenheit"
1717
Width="200"
1818
Click="Button_Click"
19-
IsEnabled="{x:Bind IsConvertButtonEnabled, Mode=OneWay}"/>
19+
IsEnabled="{x:Bind HasText(InputTextBox.Text), Mode=OneWay}"/>
2020
<TextBlock x:Name="ResultTextBlock"
2121
FontWeight="SemiBold" />
2222
</StackPanel>

WinUIGallery/Samples/ControlPages/Fundamentals/Controls/TemperatureConverterControl.xaml.cs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,19 @@
33

44
using Microsoft.UI.Xaml;
55
using Microsoft.UI.Xaml.Controls;
6-
using System.ComponentModel;
7-
using System.Runtime.CompilerServices;
86

97
namespace WinUIGallery.Samples.ControlPages.Fundamentals.Controls;
108

11-
public sealed partial class TemperatureConverterControl : UserControl, INotifyPropertyChanged
9+
public sealed partial class TemperatureConverterControl : UserControl
1210
{
13-
private bool _isConvertButtonEnabled = false;
14-
15-
public event PropertyChangedEventHandler PropertyChanged;
16-
17-
public bool IsConvertButtonEnabled
18-
{
19-
get => _isConvertButtonEnabled;
20-
set
21-
{
22-
if (_isConvertButtonEnabled != value)
23-
{
24-
_isConvertButtonEnabled = value;
25-
OnPropertyChanged();
26-
}
27-
}
28-
}
29-
3011
public TemperatureConverterControl()
3112
{
3213
this.InitializeComponent();
33-
if (InputTextBox != null)
34-
{
35-
InputTextBox.TextChanged += InputTextBox_TextChanged;
36-
UpdateButtonState();
37-
}
38-
}
39-
40-
private void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
41-
{
42-
UpdateButtonState();
4314
}
4415

45-
private void UpdateButtonState()
16+
private bool HasText(string text)
4617
{
47-
IsConvertButtonEnabled = InputTextBox != null && !string.IsNullOrWhiteSpace(InputTextBox.Text);
18+
return !string.IsNullOrWhiteSpace(text);
4819
}
4920

5021
private void Button_Click(object sender, RoutedEventArgs e)
@@ -64,9 +35,4 @@ private void Button_Click(object sender, RoutedEventArgs e)
6435
ResultTextBlock.Text = "Invalid input!";
6536
}
6637
}
67-
68-
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
69-
{
70-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
71-
}
7238
}

0 commit comments

Comments
 (0)