Databinding button content

Feb 3, 2010 at 9:42 AM

 

Hi, I am trying to bind the button content to a property in my ViewModel, when I click the button it goes through the whole INotifyPropertyChanged implementation within the ViewModel but nothing gets changed. Basicly the button content should change from 'Connect' to 'Disconnect'. I must be missing something quite important here?

Here is my button defined in the .xaml View:

<Button Grid.Column="3" x:Name="gwConnectionBarButton"
                        Content="{Binding ButtonContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        commands:Click.Command="{Binding ConnectionBarClickCommand}"
                        commands:Click.CommandParameter="{Binding ElementName=gwConnectionBarButton,Path=Content}"
                        />

and here is my ViewModel code:

 

public class ConnectionBarViewModel : IConnectionBarViewModel, INotifyPropertyChanged 
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private IUnityContainer container;
        private IRegionManager regionManager;
        private IEventAggregator eventAggregator;
        
        public IConnectionBarController Controller;
        public string ButtonContent { get; set; }

        public DelegateCommand<string> ConnectionBarClickCommand { get; private set; }

        public ConnectionBarViewModel(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator, IConnectionBarController controller)
        {
            log.Info("ConnectionVarViewModel");

            this.container = container;
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
            Controller = controller;

            ConnectionBarClickCommand = new DelegateCommand<string>(OnButtonClick); //, CanButtonClick);
            ButtonContent = "Connect";
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            log.Info("OnPropertyChanged string");
            OnPropertyChanged(new PropertyChangedEventArgs(name));
        }
 
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            log.Info("OnPropertyChanged args");
            var handler = PropertyChanged;
            if (handler != null)
            {
                log.Info("Firing handler");
                handler(this, args);
            }
        }
        #endregion


        void OnButtonClick(string pValue)
        {
            log.Info(pValue);
            if (pValue.ToLower().Equals("connect"))
            {
                if (Controller.ConnectionBarClickCommand(pValue))
                {
                    ButtonContent = "Disconnect";
                    OnPropertyChanged("gwConnectionBarButton");
                }
            }
            else
            {
                ButtonContent = "Connect";
                OnPropertyChanged("gwConnectionBarButton");
            }

        }

        bool CanButtonClick(string pValue)
        {
            return !string.IsNullOrEmpty(pValue);
        }

    }

And for good measure here is some logging:

 

2010-02-03 10:31:03,294 [9] INFO  iPoke.GatewayUX.ConnectionBar.Controllers.ConnectionBarController - (null) - ConnectionBarClickCommand
2010-02-03 10:31:03,295 [9] INFO  iPoke.GatewayUX.ConnectionBar.ViewModels.ConnectionBarViewModel - (null) - OnPropertyChanged string
2010-02-03 10:31:03,296 [9] INFO  iPoke.GatewayUX.ConnectionBar.ViewModels.ConnectionBarViewModel - (null) - OnPropertyChanged args
2010-02-03 10:31:03,296 [9] INFO  iPoke.GatewayUX.ConnectionBar.ViewModels.ConnectionBarViewModel - (null) - Firing handler

Any help much appreciated.

 

 

 

2010-02-03 10:31:03,294 [9] INFO  iProg.GatewayUX.ConnectionBar.Controllers.ConnectionBarController - (null) - ConnectionBarClickCommand
2010-02-03 10:31:03,295 [9] INFO  iProg.GatewayUX.ConnectionBar.ViewModels.ConnectionBarViewModel - (null) - OnPropertyChanged string
2010-02-03 10:31:03,296 [9] INFO  iProg.GatewayUX.ConnectionBar.ViewModels.ConnectionBarViewModel - (null) - OnPropertyChanged args
2010-02-03 10:31:03,296 [9] INFO  iProg.GatewayUX.ConnectionBar.ViewModels.ConnectionBarViewModel - (null) - Firing handler

 

 

 

Feb 13, 2010 at 9:15 PM

The argument of the PropertyChangedEvent should be the name of the property you're binding to - so try changing

OnPropertyChanged("gwConnectionBarButton");

to

OnPropertyChanged("ButtonContent");