簡體   English   中英

僅當IsKeyboardFocusWithin屬性具有錯誤值時才觸發LostFocus事件

[英]Fire LostFocus event only when IsKeyboardFocusWithin Property has a false value

我有的:

我有一個組合框。 這是使用MVVM風格的Command的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事件。

只需在IsKeyboardFocusWithin上使用一個交互DataTriggerfalse 它位於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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM