Я попытался использовать интерфейс INotifyPropertyChanged, но, к сожалению, он не подключился к основному пользовательскому интерфейсу.
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
private int _counter;
public int Counter
{
get { return _counter; }
set
{
_counter = value;
OnPropertyChanged();
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ICommand _clickCommand;
public ICommand ClickCommand
{
get
{
return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), () => CanExecute));
}
}
public bool CanExecute
{
get
{
// check if executing is allowed, i.e., validate, check if a process is running, etc.
return true;
}
}
public void MyAction()
{
Counter++;
}
.xaml файл
<Window.DataContext>
<local:viewmodel/>
</Window.DataContext>
<Grid>
<Label Content="{Binding Counter}" HorizontalAlignment="Left" Margin="218,128,0,0" VerticalAlignment="Top"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="300,176,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ClickCommand}"/>
</Grid>
Моя идея - когда я нажимаю кнопку => следует добавить 1 на счетчик, но интерфейс не обновляется. Я не знаю, как обновить пользовательский интерфейс с помощью команды и INotifyPropertyChanged?