WPF Databind数据现在显示

时间:2018-06-28 21:18:28

标签: xaml mvvm datagrid

我对MVVM风格很陌生。通过在XAML背后的代码中编写大量代码,我已经能够完成过去的所有工作。但是,我正在尝试将所有内容都转换为MVVM。

问题: 我无法将数据网格绑定到我的ObservableCollection。当我调试并遍历代码时,ObservableCollection列表已正确设置,这意味着,我可以查看变量中的数据,并显示我要添加的数据。但是,我的数据网格不显示数据。阅读后发现,如果我使用ObservableCollection,则不需要INotifyPropertyChanged。但是因为那行不通,所以我现在确实有一个。可悲的是,那仍然没有填充我的数据网格。

感谢您能提供的任何帮助。

最后,我将有一个成员类型列表,我需要数据网格来填充有关每个成员的数据。数据来自会员类。

Xaml:

<DataGrid ItemsSource="{Binding Source=PHList}"  AutoGenerateColumns="False">
        <DataGrid.Columns>
             <DataGridTextColumn Header="Member Name" Binding="{Binding Member_Name}"/>
             <DataGridTextColumn Header="Medicaid ID" Binding="{Binding Medicaid_ID}"/>
       </DataGrid.Columns>
</DataGrid>

优先健康模式:

        public PriorityHealthMember(string name, string id)
    {
        Member_Name = name;
        Medicaid_ID = id;
    }


    private string _Member_Name;

    public String Member_Name
    {
        get
        {
            return _Member_Name;
        }
        set
        {
            _Member_Name = value;
            OnPropertyChanged("Member_Name");
        }
    }


    private string _Medicaid_ID;

    public String Medicaid_ID
    {
        get
        {
            return _Medicaid_ID;
        }
        set
        {
            _Medicaid_ID = value;
            OnPropertyChanged("Medicaid_ID");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

入门ViewModel

private ObservableCollection<PriorityHealthMember> _PHList;

    public ObservableCollection<PriorityHealthMember> PhList
    {
        get
        {
            return _PHList;
        }
        set
        {
            _PHList = value;
            OnPropertyChanged("PhList");
        }
    }
    public OnboardingQueueViewModel()
    {
        PhList = GetOnboardingQueueList();
    }


    private ObservableCollection<PriorityHealthMember> GetOnboardingQueueList()
    {
        ObservableCollection<PriorityHealthMember> list = new ObservableCollection<PriorityHealthMember>();

        list.Add(new PriorityHealthMember("Andrews, Nicholas", "M123456"));//Testing for now. Will add the Business Logic after binding works. 

        return list;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。在我的xaml中,我有“ PHList”,但我的财产是“ PhList”