2016-06-10 35 views
0

我有什么:消防LostFocus事件时IsKeyboardFocusWithin酒店有假值

我有一个组合框。这里是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> 

并在视图模型:

//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。

我想要什么:

相反,我想火LostFocus事件只有当IsKeyboardFocusWithin ComboBox控件的属性为false。

回答

1

只要使用DataTrigger就可以了,IsKeyboardFocusWithinfalse。它位于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> 
+0

但是我怎么能在DataTrigger中使用触发器? – Vishal

+0

DataTriggers无所不能。 –

+0

谢谢H.按预期工作。 – Vishal