我有一个自定义 WPF 控件,它继承自ListView
. 此控件中的每个ListViewItem
控件都可以包含其他此类控件,并且可以无限地继续。
自定义ListView
子类具有三个CommandBinding
s - 剪切、复制和粘贴,以及ContextMenu
发出这些命令中的每一个的一个。所有工作都在顶层按预期工作 - 我可以剪切、复制和粘贴。但是,如果我右键单击其中一个嵌套的ListView
-descendant 控件并选择粘贴(尽管其他两个的工作方式相同),则会发生以下情况之一:
如果控件没有 selected ListViewItem
s,则CommandExecuted
触发事件,顶部ListView
为CommandTarget
. 如果控件有一个 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>
MainWindow
XAML:
<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”。