c# - 从MainViewModel(WPF,C#)访问属性

标签 c# wpf xaml mvvm

我的类FrontModel包含XAML的一些字段:

public class FrontModel()
{
  public static string LoginName { get; set;}
  public static string userPass;
  public static string Domain { get; set;}
}

进入ViewModel,我尝试连接到FrontModel
public class MainViewModel
{
  FrontModel fm = new FrontModel();

  public MainViewModel()
  {
   ...
   fm.LoginName = Environment.UserName.ToString();//error
  }  
}

但是我无法进入我的领域。我在这里做什么错了?

enter image description here

我知道LoginName {get; set;}可以直接在MainViewModel中使用,然后就可以了,但是我正在尝试将其移到单独的类中。

最佳答案

这是因为您在FrontModel的非静态实例上引用了static属性。尝试:

FrontModel.LoginName = Environment.UserName.ToString();

或者,如果该属性不需要是静态的,则删除static

关于c# - 从MainViewModel(WPF,C#)访问属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54036454/

相关文章:

xaml - 当子控件变换出其父网格或边框时隐藏子控件

c# - 从 WPF 中的 MainWindow 访问 UserControl 中的对象

javascript - 将数据从.net应用程序发送到 Electron 应用程序

wpf - 无法通过 setter 设置 System.Windows.Controls.MenuItem.Icon

c# - Bing API 图像搜索过滤器

wpf - 在设置中存储单选按钮选择

wpf - 如何在 Visual Studio 中提取默认控件模板?

wpf - 属性名称不适用于我的数据网格列绑定(bind)

C#/WPF : PropertyChanged for all Properties in ViewModel?

c# - TransactionScope helper ,无误地耗尽连接池 - 帮助?