Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

winforms - System Windows Forms - binding list of items to List View control

I am trying to use MVVM with a System Windows Form interface on an Excel VSTO plug in

I have created my API, and my model, and my view model

I have successfully tested that I can use OnPropertyChanged to update a label on the form

I am now trying to populate a CheckedListBox with a named property from a List object in the view model

While my code does not throw an error, it doesn't display the desired output either

This is a code sample from the View

        projects_label.DataBindings.Add(new Binding("Text", Globals.ThisAddIn.TFSConfigViewModel, "LabelText"));

        projects_list_box.DataBindings.Add(new Binding("Text", Globals.ThisAddIn.TFSConfigViewModel, "ListOfProjectsFromVM.value.name"));

The first example works fine. The second doesn't display the text in the list

I would have thought the Property would be 'Label' but I get an error on anything but 'Text'

This is the model

public class TFS_Project
{
    public string id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string url { get; set; }
    public string state { get; set; }
    public int revision { get; set; }
    public string visibility { get; set; }
}

public class TFS_Projects
{
    public int count { get; set; }
    public List<TFS_Project> value { get; set; }
}

This is from the View Model

    public Models.TFS_Projects ListOfProjectsFromVM

    {

        get
        {

            return _listOfProjectsFromVM;

        }

        set

        {

            _listOfProjectsFromVM = value;
            OnPropertyChanged(nameof(ListOfProjectsFromVM));

        }

    }
question from:https://stackoverflow.com/questions/65919009/system-windows-forms-binding-list-of-items-to-list-view-control

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I figured it out, by breaking down the binding command, which let me understand the error more clearly. The solution was this

        projects_list_box.DataSource = Globals.ThisAddIn.TFSConfigViewModel.ListOfProjectsFromVM.value;
        projects_list_box.DisplayMember = "name";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...