我有文本块,我想通过他的绑定属性将其从假变为真。 该属性已更改为 true,但文本框的文本保持为 false。 我怎样才能正确地做到这一点。 感谢您的帮助。
<TextBlock x:Name="resBlock" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="250" Height="50" Text="{Binding Source={StaticResource Locator}, Path=Main.Result}" TextAlignment="Center" FontSize="30" />
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
LoginCommand = new RelayCommand(Login);
user = new User();
}
DataService service = new DataService();
public User user { get; set; }
public bool Result { get; set; }
public ICommand LoginCommand { get; }
public async void Login()
{
Result = await service.LoginAsync(user); // get True
}
}
答案 0 :(得分:0)
要更改视图模型的控制量,您必须实现 INotifyPropertyChanged
接口。
将 MainViewModel 更改为:
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
public MainViewModel()
{
LoginCommand = new RelayCommand(Login);
user = new User();
}
DataService service = new DataService();
public User user { get; set; }
public ICommand LoginCommand { get; }
public async void Login()
{
Result = await service.LoginAsync(user); // get True
}
private bool result;
public bool Result
{
get { return result; }
set
{
result = value;
OnPropertyChange(nameof(Result));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}