Щелчок левой кнопкой мыши не связан со списком внутри всплывающего wpf - PullRequest
0 голосов
/ 09 апреля 2019

У меня есть всплывающее окно, внутри которого есть элемент управления списком. Мне нужно связать событие щелчка левой кнопкой мыши с элементами в списке. Я написал

<Popup StaysOpen="False" IsOpen="{Binding VisibiltyAttr}" PlacementTarget="{Binding ElementName=InputText}" HorizontalAlignment="Center" AllowsTransparency="True"  >
    <ListBox ItemsSource="{Binding Collection}" SelectedIndex="{Binding Path=ItemIndex, Mode=TwoWay}"  HorizontalAlignment="Center" VerticalAlignment="Bottom">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonUp">
                            <i:InvokeCommandAction  Command="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, 
                                        Path=DataContext.SelectionCommand}"  CommandParameter="{Binding}"></i:InvokeCommandAction>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Popup>

До того, как я добавил всплывающее окно, событие работало нормально.

1 Ответ

0 голосов
/ 09 апреля 2019

Используйте команду реле вместо команды делегата

RelayCommand.cs

public class RelayCommand : ICommand
{
    private Action<object> execute;
    private Predicate<object> canExecute;
    private event EventHandler CanExecuteChangedInternal;
    public RelayCommand(Action<object> execute)
        : this(execute, DefaultCanExecute)
    {
    }
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException(nameof(execute));
        }
        if (canExecute == null)
        {
            throw new ArgumentNullException(nameof(canExecute));
        }
        this.execute = execute;
        this.canExecute = canExecute;
    }
    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
            CanExecuteChangedInternal += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
            CanExecuteChangedInternal -= value;
        }
    }
    public bool CanExecute(object parameter)
    {
        return canExecute != null && canExecute(parameter);
    }
    public void Execute(object parameter)
    {
        execute(parameter);
    }
    public void OnCanExecuteChanged()
    {
        EventHandler handler = CanExecuteChangedInternal;
        handler?.Invoke(this, EventArgs.Empty);
    }
    public void Destroy()
    {
        canExecute = _ => false;
        execute = _ => { };
    }
    private static bool DefaultCanExecute(object parameter)
    {
        return true;
    }
}

YourViewModel.cs

private ICommand selectionCommand;
public ICommand SelectionCommand
{
    get { return selectionCommand ?? (selectionCommand = new RelayCommand(Selected, CanSelect)); }
    set { selectionCommand = value; }
}

private void Selected(object obj)
{
}
...