Andrew Simpson
Andrew Simpson

Reputation: 7324

My ViewModel does not bind

I am trying to bind my model to my UI.

I am using the singleton pattern.

This is my 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>

my 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");
            }
        }
     }
 }

I feel the error lies here:

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

as ide error tellms not found.

What am i not seeing?

Upvotes: 0

Views: 37

Answers (1)

Andrew Simpson
Andrew Simpson

Reputation: 7324

I worked it out. I needed this in my XAML:

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

Upvotes: 1

Related Questions