WPF TwoWay Binding to a static class Property

前端 未结 3 2148
遥遥无期
遥遥无期 2020-12-01 20:25

There\'s no problem if Mode=OneWay, but I have this: Class:

namespace Halt
{
    public class ProjectData
    {
            public static string Username {ge         


        
3条回答
  •  粉色の甜心
    2020-12-01 20:38

    When I have to bind to a static property I use a ViewModel that has a property that gets and sets on the static property, for example

    public class ProjectData
    {
            public static string Username {get;set;}
    }
    
    public class ViewModel {
       public string UserName {
          get{ return ProjectData.Username ; }
          set { ProjectData.Username  = value; }
       }
    }
    

    Then I set an instance of ViewModel as the UI DataContext.

提交回复
热议问题