【问题标题】:Disable list box CTRL+C & CTRL+X in D&D在 D&D 中禁用列表框 CTRL+C 和 CTRL+X
【发布时间】:2014-01-13 08:22:24
【问题描述】:

我有一个带有拖放选项到文本框的列表视图。 我需要禁用在列表框中使用 CTRL+CCTRL +X 的功能。 我不希望它被键盘触发。在 WPF 中是否有阻止它的选项?

  <ListBox  x:Name="Lst" IsSelected="False"  Height="115" Width="150" ItemsSource="{Binding UsCollectionView}" 
             SelectionChanged="listbox_SelectionChanged" AllowDrop="True" PreviewDrop="ListBox_PreviewDrop" 
    </ListBox>


private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (sender is ListBox)
    {
        var listBox = sender as ListBox;
        if (e.AddedItems.Count == 1)
        {
            if (listBox.SelectedItem != null)
            {
                var mySelectedItem = listBox.SelectedItem as User;
                if (mySelectedItem != null)
                {
                    DragDrop.DoDragDrop(listBox, mySelectedItem.Name, DragDropEffects.Copy | DragDropEffects.Move);
                }
            }
        }
    }

}

【问题讨论】:

    标签: c# wpf xaml mvvm


    【解决方案1】:

    有很多方法可以做到这一点。一种方法是处理UIElement.PreviewKeyDown Event,检测相关键,然后将e.Handled属性设置为true

    private void ListBoxPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {
            // Ctrl Key is pressed
            if (e.Key == Key.C || e.Key == Key.X) e.Handled = true;
        }
    }
    

    【讨论】:

    • 既然我是这个主题的新手,我应该将它绑定到 textBOx xaml 吗?这个事件应该如何触发?非常感谢!
    • 您说您希望阻止用户按列表框上的那些键,所以我会将此处理程序...附加到ListBox。您可以通过 XAML 或代码执行此操作,这取决于您。
    猜你喜欢
    • 2013-03-17
    • 2014-02-11
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多