变量不会更改INotifyPropertyChanged WPF

时间:2014-05-11 16:04:20

标签: c# wpf

我有这段代码:

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并从所选目录中获取新匹配。但目录是默认状态 - &#34; currentMatches&#34;。它永远不会改变。你能说出错误吗?

1 个答案:

答案 0 :(得分:0)

我想你需要进行一次改变

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);
}