变量在INotifyPropertyChanged WPF上没有改变

本文关键字:改变 WPF INotifyPropertyChanged 变量 | 更新日期: 2023-09-27 18:02:43

我有这样的代码:

private ObservableCollection<MatchesViewModel> matches;
private ObservableCollection<string> listWithLists;
private string directoryName = "currentMatches";
private void HandleLoadListClicked(object obj)
{
    directoryName = selectedItem;
    matches = DataGetterAndSetter.GetMatches("Lists''" + directoryName + ".xml");
    MainPage window = new MainPage();
    window.Show();
}
public IEnumerable<MatchesViewModel> Matches
{
    get
    {
        matches = DataGetterAndSetter.GetMatches("Lists''"+ directoryName + ".xml");
        return this.matches;
    }
    set
    {
        if (matches == null)
        {
            matches = new ObservableCollection<MatchesViewModel>();
        }
        matches.Clear();
        foreach (var item in value)
        {
            matches.Add(item);
        }
        this.OnPropertyChanged("Matches");
    }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
    if (this.PropertyChanged != null)
    {
        var propertyChangedEventArgs = new PropertyChangedEventArgs(propertyName);
        this.PropertyChanged(this, propertyChangedEventArgs);
    }
}

当我在应用程序中按下按钮时,HandleLoadListClicked方法发生。它将directoryName变量更改为所选项的内容。然后,它转到属性Matches并从所选目录获取新的匹配项。但是目录处于默认状态-"currentMatches"。它永远不会改变。你能告诉我出了什么问题吗?

变量在INotifyPropertyChanged WPF上没有改变

我想你需要一个这样的改变

private void HandleLoadListClicked(object obj)
{
    directoryName = selectedItem;
    this.Matches = DataGetterAndSetter.GetMatches("Lists''" + directoryName + ".xml");
    MainPage window = new MainPage();
    window.Show();
}

private void HandleLoadListClicked(object obj)
{
    directoryName = selectedItem;
    matches = DataGetterAndSetter.GetMatches("Lists''" + directoryName + ".xml");
    MainPage window = new MainPage();
    window.Show();
    this.PropertyChanged(this, propertyChangedEventArgs);
}