Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to implement the INotifyPropertyChanged.
Please help



View:
<listboxitem foreground="Red" fontfamily="Verdana" fontsize="12" fontweight="Bold" verticalalignment="Center">
                    <checkbox name="baseMapChkBox" ischecked="{Binding IsBaseMapChecked,Mode=TwoWay}" horizontalalignment="Center" verticalalignment="Center">
                            <textblock text="Base Map Layer"></textblock>
                    </checkbox>
                </listboxitem>

Viewmodel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.ComponentModel;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace CoM_SWBaseMapModule
{
    public class BaseMapViewModel : ViewModelBase, INotifyPropertyChanged
    {
        #region Members
        private ICommand baseMapCheckCommand;
        private BaseMapService baseMapService;
        private bool isbaseMapChecked;
        private object x=null;
        #endregion

        #region Properties
        public BaseMapView BaseMapView { get; private set; }
        public MapService MapService { get; private set; }
       
        public  bool IsBaseMapChecked
        {

            get { return isbaseMapChecked; }
            set
            {
                isbaseMapChecked = value;
                OnBaseMapCheck(x);
                OnCheckPropertyChanged("IsChecked");
            }
        }
        #endregion


        #region Methods
        public BaseMapViewModel(BaseMapView baseMapView, MapService mapService)
        {
            BaseMapView = baseMapView;
            MapService = mapService;
            baseMapService = new BaseMapService();
            IsBaseMapChecked = true;

        }
        #endregion
        #region CommandProperties
       
        #endregion

        #region Private Methods
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnCheckPropertyChanged(string propertyName)
        {
           if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
        }


        private void OnBaseMapCheck(object p)
        {
            if (isbaseMapChecked)
                baseMapService.LoadBaseMap(MapService, true);
            if (!isbaseMapChecked)
                baseMapService.LoadBaseMap(MapService, false);
        }

        #endregion

    }
}
Posted
Updated 10-Jun-10 15:51pm
v2
Comments
PSK_ 10-Jun-10 21:51pm    
Please use pre tag for code block.
jeetuoslo 11-Jun-10 0:43am    
What pre tag and where- in the Xaml ?

1 solution

Hi,

Add the UpdateSourceTrigger to the binding.For UI elements the default UpdateSourceTrigger is LostFocus.You can set the UpdateSourceTrigger=PropertyChanged inside the binding as,

XML
<checkbox name="baseMapChkBox" ischecked="{Binding IsBaseMapChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" horizontalalignment="Center" verticalalignment="Center">


One more point, "IsBaseMapChecked" is the property that you defined and triggered the "IsChecked" property inside the propertychange method,you have to trigger the "IsBaseMapChecked" property inside the PropertyChanged event I think.ie.;

PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
become
PropertyChanged(this, new PropertyChangedEventArgs("IsBaseMapChecked"));


Regards,
Vineeth
 
Share this answer
 
v2
Comments
jeetuoslo 11-Jun-10 10:03am    
Nothing happened on the UI, the checkbox is not checked when I debug the application nor there is any the method private void OnBaseMapCheck(object p)
{
if (isbaseMapChecked)
baseMapService.LoadBaseMap(MapService, true);
if (!isbaseMapChecked)
baseMapService.LoadBaseMap(MapService, false);
}
fired on the change of click status.
jeetuoslo 11-Jun-10 10:07am    
public event PropertyChangedEventHandler PropertyChanged; PropertyChanged remains null so the event method is never fired.
jeetuoslo 11-Jun-10 12:12pm    
Got the solution, forgot the set the data context of the view

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900