我的ViewModel没有绑定

时间:2016-01-09 12:09:39

标签: wpf model-binding

我正在尝试将我的模型绑定到我的UI。

我正在使用单身模式。

这是我的用户界面:

<UserControl x:Class="InformedWorkerClient.UserControls.ucCustomerDetails"
             xmlns:viewModels="clr-namespace:InformedWorkerDataService.Common;assembly=InformedWorkerDataService"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:InformedWorkerClient.UserControls"
             mc:Ignorable="d"     
          d:DataContext="{d:DesignInstance viewModels:ActiveCustomer, IsDesignTimeCreatable=True}"
    d:DesignHeight="580" d:DesignWidth="460">
        <UserControl.DataContext>
        <local:ActiveCustomer />
        </UserControl.DataContext>
        <Grid>
             <TextBlock Foreground="White"  Text="{Binding  CustomerRef}" />
</Grid>
</UserControl>

我的VM:

using System.ComponentModel;

namespace InformedWorkerDataService.Common
{
    public class ActiveCustomer : INotifyPropertyChanged
    {
        public ActiveCustomer() { }
        private static ActiveCustomer _instance;
        public static ActiveCustomer Instance
        {
            get { return _instance ?? (_instance = new ActiveCustomer()); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }



        public string CompanyRef { get; set; }

        private string customerref = "hello andy288";
        public string CustomerRef
        {

            get { return customerref; }
            set
            {
                customerref = value;
                OnPropertyChanged("CustomerRef");
            }
        }
     }
 }

我觉得错误就在这里:

<UserControl.DataContext>
    <local:ActiveCustomer />
</UserControl.DataContext>

因为未找到ide错误。

我没看到什么?

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。我在XAML中需要这个:

<UserControl.DataContext>            
    <viewModels:ActiveCustomer/>
</UserControl.DataContext>