【问题标题】:Assign UserControl declared variable, into another UserControl将 UserControl 声明的变量分配到另一个 UserControl
【发布时间】:2016-07-22 19:02:41
【问题描述】:

试图找到一些对类似问题的参考,但没有一个能引导我解决我的问题。 我有一个 UserControl - ProjectWizardProgressBar,我在其中声明并使用了一个变量。它看起来像这样:

namespace Todiste.Views.Proyectos.ProjectWizardProgressBar
public partial class ProjectWizardProgressBar : UserControl, INotifyPropertyChanged
{
private string _test;
    public string test
    {
        get { return _test; }
        set
        {
            if (value != _test)
            {
                _test = value;
                OnPropertyChanged("test");
            }
        }
    }
public ProjectWizardProgressBar()
    {
        InitializeComponent();
        DataContext = this;            
        test = "This is a test dummy";
    }
...
}

我在不同的 UserControl - NewProjectStep1View 中使用此用户控件,我必须在其中重新分配变量 test。以这样的方式:

namespace Todiste.Views.Proyectos.NewProjectWizard
{

public partial class NewProjectStep1View : UserControl, INewProjectWizardStep
{
public void OnStepLoaded()
    {
        ProjectWizardProgressBar.ProjectWizardProgressBar.test = "This is an updated test text" ;            

    }
...
}

此时,当我像这样重新分配变量时,我得到:
“非静态字段、方法或属性 'ProjectWizardProgressBar.test' 需要对象引用”。

非常感谢您帮助我理解这种类型的数据绑定。

【问题讨论】:

    标签: c# wpf data-binding user-controls


    【解决方案1】:

    问题的正确答案是:
    在必要的控制器 (NewProjectStep1View) 上,使用更新的变量值创建原始 UserController (ProjectWizardProgressBar) 的新子实例。 代码如下:

    namespace Todiste.Views.Proyectos.NewProjectWizard
    {
    
    public partial class NewProjectStep1View : UserControl, INewProjectWizardStep
    {
    public void OnStepLoaded()
    {
        ProjectWizardProgressBar progressBar = new ProjectWizardProgressBar();
        progressBar.test = "This is an updated test text";    
    
    }
    ...
    }
    


    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2019-09-05
      • 2022-12-11
      • 2021-07-26
      相关资源
      最近更新 更多