I have the following sample which mimics an application with a menu and a status bar button doing the same thing.
If I only execute the command from the menu the toolbar button gets updated fine but if I start using the toolbarbutton the menu and the toolbar gets out of synch. The same happens if I do the other way around starting with the toolbar button the menu gets updated until I use the menu for the fist time.
What I’m missing?
C#:
using System.ComponentModel;
using System.Windows.Input;
using Picis.Wpf.Framework.Commands;
namespace CheckTest
{
public partial class Window1 : INotifyPropertyChanged
{
private bool _state;
public ICommand ChangeStateCommand { get; private set; }
public bool State
{
get
{
return _state;
}
set
{
if (_state != value)
{
_state = value;
this.OnPropertyChanged("State");
}
}
}
public Window1()
{
this.ChangeStateCommand = new DelegateCommand<bool>(ExecuteChangeState);
InitializeComponent();
this.DataContext = this;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
private void ExecuteChangeState(bool state)
{
this.State = !state;
}
}
}
XAML:
<Window x:Class="CheckTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<Menu IsMainMenu="True">
<MenuItem IsChecked="{Binding State, Mode=OneWay}" Command="{Binding ChangeStateCommand}" CommandParameter="{Binding State}" Header="Test" IsCheckable="True" />
</Menu>
<ToggleButton IsChecked="{Binding State, Mode=OneWay}" Command="{Binding ChangeStateCommand}" CommandParameter="{Binding State}" Content="Test2" />
</StackPanel>
</Window>
I have imported your code into a new project.
I had to import my implementation of Delegate command which is basically the non generic version of relay command by Josh Smith and everything works fine (the two items remain synced no matter which order they are clicked).
The only update to the code I made was the following which basically accommodates the fact that my version of delegate command is non-generic.
The xaml remains unchanged from your post (My understanding is that the Path parameter is implicit in the binding so it makes no difference if it is specified or not).
I’m suspecting the issue could be that there is something wrong with the implementation of DelegateCommand you are working with. Alternatively, the sample you posted might be missing something from your actual application that is giving you the problem.
Are you certain the code you have posted adequately models the problem you are experiencing?