WPF。如何以挑起文字变化在按一下按钮

0

的问题

我的工作WPF项目。并且我遇到一个问题不会拿起最后文本的变化,当我点击救按钮。

我的成分是设置这样的:

  1. 一个工具栏看栏虚拟机,其中有保存按钮。 按钮 点击事件中被绑到一Icommand.
  2. 一种形式看到一堆文字,一sepreate虚拟机。
  3. 该工具条景和形式来看都是单独的文件。
  4. 我所有的虚拟机继承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();
    }
}

任何人都不会知道的一清洁的方式,以确保重点是遗失或一种触发机事件的可绑定的性质

2-way-object-databinding c# mvvm prism
2021-11-23 08:41:19
1

最好的答案

1

你应该使用UpdateSourceTrigger=引发

<TextBox Text="{Binding TextValue, UpdateSourceTrigger=PropertyChanged}"/>

2021-11-23 08:59:41

事实上,点击按钮装置损失的重点文本,所以它 应该 有更新。
Haukinger

@Haukinger嗯...
dmitriy

@Haukinger,我认为就是这种情况,但可悲的是,没有,它不会触发该失去焦点
Just another Dev

@Haukinger,我不知道如果这是因为该工具条,文本形式是在单独的控制? 这没有意义。
Just another Dev

@JustanotherDev它可以肯定的来源,如果工具栏和形式的控制,有独立视图模型。
Jeffery

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................