繁体   English   中英

无法使数据绑定正常工作(我读了很多帖子,也弄不清楚我在做什么错)

[英]Can not get Databinding to work (I Have read many posts and can not figure out what I am doing wrong)

我已经为INotifyPropertyChanged创建了这个基类。

namespace BASECLASSES.HelperClasses
{
    public class NotifyPropChangedBase : INotifyPropertyChanged
    {


    /// <summary>
    /// The propertyChanged Event to raise to any UI object
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;


    /// <summary>
    /// The PropertyChanged Event to raise to any UI object
    /// The event is only invoked if data binding is used
    /// </summary>
    /// <param name="propertyName"></param>
    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);

            handler(this, args);
        }

    }

}

}

我创建了一个ViewModel,它在这里实现了基类。

  public class CheckoutVM : BASECLASSES.HelperClasses.NotifyPropChangedBase
{




    private string fullName ;

    public string FullName
    {
        get { return fullName; }
        set
        {
            if (fullName != value)
            {
                fullName = value;

                RaisePropertyChanged("FullName");
            }

        }
    }






}   


}

}

在XAML中,我为类定义了一个命名空间。

 xmlns:bcbcns="clr-Namespace:BASECLASSES.HelperClasses;assembly=BASECLASSES"

我已经定义了一个窗口资源...

  <Window.Resources>
      <m:CheckoutVM x:Key="chkOutViewModel"  />
  </Window.Resources> 

将DataContext设置为主网格...

   <Grid DataContext="{Binding Source={StaticResource chkOutViewModel}}">

将标签内容的路径设置为...

  <Label Name="txtContactCheckingOut"  Content="{Binding Path=FullName}"/>

接下来,我用此代码设置标签...

 List<GET_CONTACT_Result> contactResultList = modsqlContact.GET_CONTACT(contactID);
 CheckoutVM checkOutContact = new CheckoutVM();
 checkOutContact.FullName = contactResultList[0].cFULLNAME;

但是标签未设置。

如果我像这样向ViewModel添加构造函数...

    public CheckoutVM()
       {
          FullName = "XXXXXXXXXXXXXXXXX";
       }

标签设置为XXXXXXXXXXXXXXXXX,因此绑定似乎正确。

看起来该处理程序始终为null。 请帮忙!!!! 我究竟做错了什么???

问题是您的checkOutContact与您的控件中使用的CheckoutVM实例不是同一实例。

解决此问题的一种方法是在Window的后台代码中设置视图模型。 像这样:

public CheckoutVM ViewModel
{
    get { return (CheckoutVM) DataContext; }
    set { DataContext = value; }
}

然后,从网格中删除DataContext。 默认情况下,Window中的控件将使用与Window相同的DataContext。

<Grid>
    ...
    <Label Name="txtContactCheckingOut"  Content="{Binding FullName}"/>
    ...
</Grid>

您可以像这样初始化窗口:

YourWindow yourWindow = new YourWindow();
yourWindow.ViewModel = new CheckoutVM();
yourWindow.ViewModel.FullName = contactResultList[0].cFULLNAME;

这应该可以解决问题。

通过Resources属性(资源的合并字典),您可以在后面的代码中找到资源。 您可以通过提供的密钥访问该商品。 假设后面的Window代码中包含用于设置FullName属性的示例代码,则以下代码应允许您将值更新为绑定实例。

List<GET_CONTACT_Result> contactResultList = modsqlContact.GET_CONTACT(contactID);
var contact = (CheckoutVM)Resources["chkOutViewModel"];
contact.FullName = contactResultList[0].cFULLNAME;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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