我有以下示例,它模仿具有菜单和状态栏按钮的应用程序做同样的事情。
如果我只从菜单执行命令,工具栏按钮会更新得很好,但是如果我开始使用工具栏按钮,菜单和工具栏就会不同步。如果我从工具栏按钮开始,菜单会更新,直到我第一次使用菜单,也会发生同样的情况。
我错过了什么?
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>