WPF交互触发器IsKeyboardFocusWithinChanged不触发

时间:2018-09-22 12:28:41

标签: c# wpf events

交互触发事件“ IsKeyboardFocusWithinChanged”不会触发,我在命令中放置了断点,但没有任何反应。触发器位于UserControl内部。命令就像是一种魅力,但正如我所说,它不是在开火。在此事件后面的代码中,一切正常。 XAML:

<UserControl x:Class="PixiEditor.Views.MenuButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:PixiEditor.Views"
         xmlns:vm="clr-namespace:PixiEditor.ViewModels"
         xmlns:helpers="clr-namespace:PixiEditor.Helpers.Behaviours"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         mc:Ignorable="d" 
         d:DesignHeight="330" d:DesignWidth="200" x:Name="menuButton" DataContext="{DynamicResource MenuButtonViewModel}">
<UserControl.Resources>
    <vm:MenuButtonViewModel x:Key="MenuButtonViewModel"/>
</UserControl.Resources>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="IsKeyboardFocusWithinChanged">
        <i:InvokeCommandAction Command="{Binding CloseListViewCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
<StackPanel Name="MainStackPanel">

    <Button Content="{Binding ElementName=menuButton,Path=Text}" Style="{StaticResource MenuButton}" HorizontalAlignment="Left" Command="{Binding OpenListViewCommand}"/>
    <ListView Visibility="{Binding ListViewVisibility}" Style="{StaticResource MenuListViewStyle}">
        <ContentPresenter Content="{Binding Item, ElementName=menuButton}"/>
        </ListView>
</StackPanel>

ViewModel

using PixiEditor.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PixiEditor.ViewModels
{
class MenuButtonViewModel : ViewModelBase
{
    public RelayCommand OpenListViewCommand { get; set; }
    public RelayCommand CloseListViewCommand { get; set; }
    private Visibility _listViewVisibility;

    public Visibility ListViewVisibility
    {
        get { return _listViewVisibility; }
        set { _listViewVisibility = value; RaisePropertyChanged("ListViewVisibility"); }
    }


    public MenuButtonViewModel()
    {
        OpenListViewCommand = new RelayCommand(OpenListView);
        CloseListViewCommand = new RelayCommand(CloseListView);
        ListViewVisibility = Visibility.Hidden;
    }

    private void OpenListView(object parameter)
    {
        ListViewVisibility = (ListViewVisibility == Visibility.Hidden) ? Visibility.Visible : Visibility.Hidden;
    }

    private void CloseListView(object parameter)
    {
        ListViewVisibility = Visibility.Hidden;
    }
}

}

0 个答案:

没有答案