2016-06-10 23 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