Databinding button content

Topics: Prism v2 - WPF 3.5
Feb 3, 2010 at 9:51 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:

 

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 backing ViewModel:

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 some log output:
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. Mark


 

 

Feb 3, 2010 at 3:51 PM

Hi Mark,

From the code above I notice that you are raising the OnPropertyChanged event passing the button’s name (“gwConnectionBarButton”) as the property name. In this case, you would need to pass the “ButtonContent” property (which is the one being updated), so the binding engine notices the update to that property and updates the button’s content:
OnPropertyChanged("ButtonContent");

You can check how this is implemented in some ViewModels of the Prism RI.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Feb 3, 2010 at 5:09 PM

Thanks, had a feeling it was something simple. Mark