无法让数据绑定工作(我读过很多帖子,无法弄清楚我做错了什么)

  • 本文关键字:弄清楚 错了 工作 数据绑定 c# wpf
  • 更新时间 :
  • 英文 :


我为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);
        }
    }
}

}

我创建了一个视图模型,它在这里实现了基类......

  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> 

将数据上下文设置为主网格...

   <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;

但标签没有设置。

如果我像这样向视图模型添加一个构造函数......

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

标签设置为

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

你的资源可以通过 Resources 属性(合并的资源字典)在代码隐藏中提供。 您的物品将通过您提供的密钥提供。 假设用于设置 FullName 属性的示例代码位于 Window 代码隐藏中,则以下代码应允许您将值更新到绑定实例。

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

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

解决此问题的一种方法是在窗口的代码隐藏中设置视图模型。像这样:

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

然后,从网格中删除数据上下文。默认情况下,窗口中的控件将采用与窗口相同的数据上下文。

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

然后像这样初始化窗口:

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

这应该可以解决问题。

相关内容

最新更新