【发布时间】:2020-01-19 17:16:39
【问题描述】:
我目前正在使用 WPF MVVM 模式实现应用程序。在那里我遇到了一个问题,我在代码隐藏(比如说 Author.xaml.cs)中,在中介方法中分配了一些属性(IsClickedYes 为 true),然后当中介返回到特定的视图模型(AuthorViewModel.cs)时,我需要从 AuthhorViewModel.cs 访问 Author.xaml.cs 中的 IsClickedYes 属性。我怎样才能做到这一点?
作者.cs
public class Author
{
private bool _isClickedYes;
public bool IsClickedYes
{
get { return _isClickedYes; }
set { _isClickedYes= value; }
}
public Author()
{
Mediator.Register("SetClickedYesProperty",SetClickedYes);
}
private void SetClickedYes(object parameter)
{
//Show a Confirm Message Dialog here then if user clicked yes set IsClickedYes property to true
_isClickedYes=true;
}
}
AuthorViewModel.cs
public class AuthorViewModel
{
//this will call the SetClickedYes method in Author.xaml.cs
Mediator.NotifyCollegue("SetClickedYesProperty",null);
//then here I need to access the IsClickedYes Property value of Author.xaml.cs
if IsClickedYes == true , then do a certain operation otherwise do nothing.
}
如果我尝试在 AuthorViwModel 中创建一个属性并在代码隐藏中设置该属性,则当它从中介返回时,它变为空。这就是为什么我在代码隐藏中创建属性并在那里分配值,并尝试从视图模型访问它。 我该如何实现这一点,还有其他方法可以实现吗?如果还有其他更好的方法可以实现这一点,请任何人指导我?任何建议将不胜感激!
【问题讨论】:
-
不知道为什么不绑定?将 IsClickedYes 属性移动到 ViewModel 之后,然后使用特殊控件绑定,您将获得所需的一切。