开发者

C# WPF MVVM: Calling an Object

开发者 https://www.devze.com 2023-02-20 18:47 出处:网络
I have a QuestionsFile class, which has questions, a name etc... Now I have created a wpf application and I have a window which has different pages. These pages inherit the BaseViewModel class. I cre

I have a QuestionsFile class, which has questions, a name etc...

Now I have created a wpf application and I have a window which has different pages. These pages inherit the BaseViewModel class. I create my pages in the WindowViewModel class (where I pass a new QuestionsFile object to the constructor and dispatch it through the constructors of the other viewmodels):

var chooseQFVM = new ChooseQuestionsFileViewModel(this.QuestionsFile);
var showChosenQFVM = new ShowChosenQuestionsFileViewM开发者_运维技巧odel(this.QuestionsFile);

var pages2 = new List<WizardControlViewBaseModel>();
pages2.Add(chooseQFVM);
pages2.Add(showChosenQFVM);

pages = new ReadOnlyCollection<WizardControlViewBaseModel>(pages2);`

All these viewmodels inherit the BaseViewModel class which has the QuestionsFile property.

so when let's say I change the QuestionsFile property of the chooseQFView variable from a combobox and I give it through - the same property in the BaseBiewModel class must be changed. So this is this code of the property in the BaseBiewModel class:

public QuestionsFile QuestionsFile
    {
        get { return qfile; }
        set 
        { 
            qfile = value;
            OnPropertyChanged("QuestionsFile");

        }
    } 

So I call the PropertyChanged event, but when i want to call this property in the next page and I debug it says that all the properties of my QuestionsFile are null..

this is the code where I change the value of the QuestionsFile property:

public OptionViewModel<QuestionsFile> SelectedFile
    {
        get
        {
            return selectedFile;
        }
        set
        {
            selectedFile = value;
            OnPropertyChanged("SelectedFile");
            this.QuestionsFile = value.GetValue();
        }
    }


From what I understand on your view you have a collection of QuestionFiles (via OptionsViewModel) that you are selecting one of them via the SelectedFile property. At this point in time you are setting the chosen QuestionFile property.

This is all good for the ChooseQuestionViewModel (which I assume contains the SelectedFile property). I am not sure this is your issue but the SelectedFile will not reflect inside the ShowQuestionsFileViewModel because now both view models refer to a different instance.

Is this your issue? That you cannot see change of the selected QuestionFile in the ShowQuestionsFileViewModel?

If so you will need to tell the other view model that it has changed via events or having it reference the ChooseQuestionViewModel so it can listen for the PropertyChanged event so you can grab the selected item from and update itself.

i.e.

//Ctor
public ShowQuestionsFileViewModel(ChooseQuestionViewModel chooseViewModel)
{
    _chooseViewModel = chooseViewModel;
    chooseViewModel.PropertyChanged += ChoosePropertyChanged
}

private void ChoosePropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if(e.Property == "SelectedFile")
    {
        this.SelectedFile = _chooseViewModel.SelecteFile;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消