我的工作WPF项目。并且我遇到一个问题不会拿起最后文本的变化,当我点击救按钮。
我的成分是设置这样的:
- 一个工具栏看栏虚拟机,其中有保存按钮。 按钮 点击事件中被绑到一Icommand.
- 一种形式看到一堆文字,一sepreate虚拟机。
- 该工具条景和形式来看都是单独的文件。
- 我所有的虚拟机继承BindableBase(第一部分的棱镜。。包)
在按一下按钮,我将采取的值形成虚拟机和拯救他们...简单和stragight前进。 一切都很好,除了经编辑的最后一个领域,点击救按钮不会触发该失去焦点的活动。 因此,财产设定的事件没有触发的。 我将要击从该领域的编辑后,然后按一下按钮。 这是我的代号:
public class ViewModel: BindableBase
{
private string _someText;
public string SomeText
{
get { return _someText; }
set { SetProperty(ref _someText, value); }
}
}
在图键
<TextBox Text="{Binding SomeText}"/>
在工具条键
<Button Command="{Binding SaveCommand}" ToolTip="Save">
</Button>
视图模型的工具栏:
public class ToolbarViewModel : BindableBase
{
private ICommand _saveCommand;
public ICommand SaveCommand
{
get
{
return _saveCommand ?? (_saveCommand = new BaseCommandHandler(() => {
//Save code
}, () => true));
}
}
}
代码BaseCommandHandler:
public class BaseCommandHandler : ICommand
{
private Action _action;
private Func<bool> _canExecute;
/// <summary>
/// Creates instance of the command handler
/// </summary>
/// <param name="action">Action to be executed by the command</param>
/// <param name="canExecute">A bolean property to containing current permissions to execute the command</param>
public BaseCommandHandler(Action action, Func<bool> canExecute)
{
_action = action;
_canExecute = canExecute;
}
/// <summary>
/// Wires CanExecuteChanged event
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Forcess checking if execute is allowed
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
return _canExecute.Invoke();
}
public void Execute(object parameter)
{
_action();
}
}
任何人都不会知道的一清洁的方式,以确保重点是遗失或一种触发机事件的可绑定的性质