仅当IsKeyboardFocusWithin属性具有false值时才触发LostFocus事件

时间:2016-06-10 14:59:59

标签: c# wpf xaml mvvm combobox

  

我有什么:

我有一个组合框。这是使用MVVM风格的命令的LostFocus:

<ComboBox .............>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <i:InvokeCommandAction Command="{Binding DataContext.OrderIdLostFocusCommand, UpdateSourceTrigger=PropertyChanged,
                                                     RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

在ViewModel中:

//Constructor
public DispatchViewModel(IEventAggregator _eventAggregator)
{
    eventAggregator = _eventAggregator;

    //Some Code

    OrderIdLostFocusCommand = new RelayCommand(Execute_OrderId_LostFocus);
}

public RelayCommand OrderIdLostFocusCommand { get; set; }

private void Execute_OrderId_LostFocus(object obj)
{
    //Some Calculations

    eventAggregator.GetEvent<MoveFocusToDatePickerEvent>().Publish(true);
}

在守则背后:

//Constructor
public DispatchView(DispatchViewModel _viewModel, IEventAggregator _eventAggregator)
{
    InitializeComponent();
    this.DataContext = _viewModel;
    _eventAggregator.GetEvent<MoveFocusToDatePickerEvent>().Subscribe(MoveFocusToDateOfDispatch);
}

private void MoveFocusToDateOfDispatch(bool obj)
{
    this.Dispatcher.BeginInvoke((Action)delegate { dpInvoice.Focus(); }, DispatcherPriority.Render);
}
  

问题:

当Combobox聚焦时,如果我尝试打开它的DropDown,那么它的lostfocus事件会触发,因此焦点移动到DataPicker。

  

我想要的是什么:

相反,我只想在ComboBox的IsKeyboardFocusWithin属性为false时触发LostFocus事件。

1 个答案:

答案 0 :(得分:1)

只需使用DataTrigger IsKeyboardFocusWithin上的互动false即可。它位于http://schemas.microsoft.com/expression/2010/interactions命名空间中。

<i:Interaction.Triggers>
    <ii:DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBox},
                                    Path=IsKeyboardFocusWithin}"
                    Value="false">
        <i:InvokeCommandAction .../>
    </ii:DataTrigger>
</i:Interaction.Triggers>