我是 Avalonia 的新手,所以我的代码应该很基本。我有 1 个窗口,其中有 1 个面板,即:
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
<TextBlock Text="{Binding Greeting}" />
<TextBox Text="{Binding Name}"/>
<Button Content="Say HI" Click="OnButtonClicked" IsEnabled="{Binding Enable}"/>
该面板具有 TextBlock、TextBox 和按钮。默认情况下不启用该按钮。我的问题是,当 textBox 的值发生变化时,如何启用它。这是我的 Model 类,其中已经包含一些基本逻辑:
class HelloViewModel : INotifyPropertyChanged
{
private string greeting = "";
private string name = "";
public bool Enable = false;
public string Greeting
{
get => greeting;
set
{
if (value != greeting)
{
greeting = value;
OnPropertyChanged();
Enable = true;
}
}
}
public string Name
{
get => name;
set
{
if(value != name)
{
name = value;
OnPropertyChanged();
Enable = true;
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}