0

我有一个自定义 WPF 控件,它继承自ListView. 此控件中的每个ListViewItem控件都可以包含其他此类控件,并且可以无限地继续。

自定义ListView子类具有三个CommandBindings - 剪切、复制和粘贴,以及ContextMenu发出这些命令中的每一个的一个。所有工作都在顶层按预期工作 - 我可以剪切、复制和粘贴。但是,如果我右键单击其中一个嵌套的ListView-descendant 控件并选择粘贴(尽管其他两个的工作方式相同),则会发生以下情况之一:

如果控件没有 selected ListViewItems,则CommandExecuted触发事件,顶部ListViewCommandTarget. 如果控件有一个 selected ListViewItem,一切正常。

这是一个已知的问题?有没有(体面的)解决方法?谢谢。

编辑根据要求,这是一个例子:

自定义ListView子类:

public class MyListView : ListView
{
    public MyListView()
    {
        this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,
            CopyCommand_Executed, CopyCutCommand_CanExecute));
        this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste,
            PasteCommand_Executed, PasteCommand_CanExecute));
        this.Background = new SolidColorBrush(Colors.Ivory);
    }

    MyListItem Binding 
    { 
        get { return this.DataContext as MyListItem; } 
    }

    private void CopyCutCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (e.Handled)
            return;

        e.CanExecute = (this.SelectedItems.Count > 0);
        e.Handled = true;
    }

    private void CopyCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        if (e.Handled)
            return;

        if (this.SelectedItems.Count < 0)
            return; // Nothing selected

        List<MyListItem> items = new List<MyListItem>();
        foreach (MyListItem str in this.SelectedItems)
            items.Add(str);

        Clipboard.SetData("Stuff", items);

        e.Handled = true;
    }

    private void PasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (e.Handled)
            return;

        e.CanExecute = Clipboard.ContainsData("Stuff");
        e.Handled = true;
    }

    private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        if (e.Handled)
            return;

        MyListView lv = this;

        List<MyListItem> strings = Clipboard.GetData("Stuff") as List<MyListItem>;
        if (strings == null)
            return;

        foreach (MyListItem s in strings)
            this.Binding.Items.Add(s);

        e.Handled = true;
    }
}

简单ListViewItem绑定类:

[Serializable]
public class MyListItem
{
    public MyListItem() { this.Items = new ObservableCollection<MyListItem>(); }
    public string Caption { get; set; }
    public ObservableCollection<MyListItem> Items { get; set; }
}

XAML for MyControl(无代码隐藏),每个的表示ListViewItem

<UserControl x:Class="TestNestedListView.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:TestNestedListView">
    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding Path=Caption}" />
            <local:MyListView Padding="10" ItemsSource="{Binding Path=Items}"/>
        </StackPanel>
    </Grid>
</UserControl>

MainWindowXAML:

<Window x:Class="TestNestedListView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestNestedListView"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:MyListItem}">
            <local:MyControl DataContext="{Binding}" />
        </DataTemplate>
        <Style TargetType="local:MyListView">
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="VerticalContentAlignment" Value="Stretch" />
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <local:MyListView x:Name="lv" ItemsSource="{Binding Path=Items}">
        <local:MyListView.ContextMenu>
            <ContextMenu>
                <MenuItem Command="Copy" />
                <MenuItem Command="Paste" />
            </ContextMenu>
        </local:MyListView.ContextMenu>
    </local:MyListView>
</Window>

MainWindow代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MyListItem root = new MyListItem();

        MyListItem item = new MyListItem() { Caption = "Item 1" };
        item.Items.Add(new MyListItem() { Caption = "Subitem 1.1" });
        root.Items.Add(item);
        root.Items.Add(new MyListItem() { Caption = "Item 2" });
        root.Items.Add(new MyListItem() { Caption = "Item 3" });
        root.Items.Add(new MyListItem() { Caption = "Item 4" });

        item = new MyListItem() { Caption = "Item 5" };
        item.Items.Add(new MyListItem() { Caption = "Subitem 5.1" });
        item.Items.Add(new MyListItem() { Caption = "Subitem 5.2" });

        root.Items.Add(item);

        lv.DataContext = root;
    }
}

要查看此操作,请右键单击并复制任何项目。然后右键单击ListView“项目 2”下的矩形(它是 )并单击粘贴。请注意,新项目将被粘贴为顶层中的最后一个项目。右键单击“Subitem 1.1”下的矩形,注意该项粘贴为“Item 1”的子项,而不是“Subitem 1.1”。

4

1 回答 1

1

根据这个列表视图必须有焦点正确处理命令。我通过将您的类型的空列表视图添加到表单和文本框来观察到这个缺陷。然后,当我关注文本框并在剪贴板中有一些文本粘贴命令时,即使在您的列表视图中,也不会触发任何 CanExecute 方法,并且选择粘贴命令将粘贴到文本框。这确实看起来像 wpf 中的一个错误,我发现很少有文章表明它是已知的,但我没有找到它的票。

在 WPF 错误中禁用 ContextMenus

为什么我的 ContextMenu 项目被禁用?

编辑

作为您的方案的解决方法,只需添加:

protected override void OnMouseDown(MouseButtonEventArgs e)
{
    base.OnMouseDown(e);
    Dispatcher.BeginInvoke(new Func<bool>(Focus));
}

到你的MyListView班级。

于 2012-08-14T06:40:17.830 回答