简体   繁体   中英

Label content in custom UserControl not updating

I've created a custom UserControl which consists of 12 labels.

在此处输入图片说明

Now, when the program loads, the labels with "Default" as the content need to be changed.

// This is called in my main forms constructor, right before InitializeComponent()
public void ShowRigInfo()
{
    // This is here because if I try to call PopluateLabels, I get an "Object not
    // set to an instance of object" error
    grdRigInfo = new RigInfoGrid();

    var contractor = SplitString("contractor", _rigInfo);
    var projectName = SplitString("projectname", _rigInfo);
    var location = SplitString("location", _rigInfo);
    var operatorName = SplitString("operator", _rigInfo);
    var rigName = SplitString("rigsite_name", _rigInfo);
    var rigManager = SplitString("rigmanager", _rigInfo);

    grdRigInfo.PopulateLabels(contractor, projectName, location, operatorName,
                              rigName, rigManager);
    }

// A public method of my custom UserControl to update label content
public void PopulateLabels(string contractor, string project, string location, 
                           string operatorName, string rigName, string manager)
{
    lblContractor.Content = contractor;
    lblProjectName.Content = project;
    lblLocation.Content = location;
    lblOperator.Content = operatorName;
    lblRigName.Content = rigName;
    lblRigManager.Content = manager;            
}

My question is, how can I get the labels to update upon program start up? Thanks for any and all help.

EDIT

I have tried to call ShowRigInfo() both before and after InitializeComponent() of my main form. Neither of them changed the labels.

EDIT 2

Okay, I solved this before I actually saw the answers. What I did was moved my ShowRigInfo() into my custom UserControl, instead of my main form. I don't know why I didn't do that from the start, but there it is. I'll be looking into the DataBinding portion of the answers. Thank you guys.

Well, because this is WPF, I would recommend binding those labels to properties in some sort of model which supports (implements) INotifyPropertyChanged.

If you Google with those words, you'll come a long way.

为什么不在Loaded事件处理程序中尝试呢?

AD.Net is right: Place your initial setup into the Loaded event. In the Constructor, you can play around with local variables, but you cannot typically play with visual elements. once the "Loaded" event is fired, all your UI components should be available to use.

However, I strongly encourage you to use DataBinding and DataContext on your Labels instead of populating them through a public method. Eventually, if you start working with datagrids, treeviews and listviews, you will realize how the WPF system is built around Binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM