我有一个继承自ListView
的自定义WPF控件。此控件中的每个ListViewItem
都可以包含其他此类控件,这可以无限制地继续。
自定义ListView
子类有三个CommandBinding
s - 剪切,复制和粘贴,以及发出这些命令的ContextMenu
。所有工作都按预期在顶层 - 我可以剪切,复制和粘贴。但是,如果我右键单击其中一个嵌套的ListView
- 后代控件并选择粘贴(尽管其他两个工作相同),则会发生以下情况之一:
如果控件没有选中ListViewItem
个,则CommandExecuted
事件将以ListView
作为CommandTarget
进行触发。
如果控件具有选定的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; }
}
MyControl
的XAML(无代码隐藏),每个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;
}
}
要查看此操作,请右键单击并复制任何项目。然后右键单击“项目2”下的矩形(它是ListView
)并单击粘贴。请注意,新项目将粘贴为顶级中的最后一项。右键单击“Subitem 1.1”下的矩形,并注意该项目被粘贴为“Item 1”的子项,而不是“Subitem 1.1”。
答案 0 :(得分:1)
根据this列表视图必须专注于正确处理命令。我通过在表单和文本框中添加类型的空列表视图来观察此缺陷。然后,当我关注文本框并在剪贴板中时,即使在列表视图中,某些文本粘贴命令也处于活动状态,但是没有触发任何CanExecute方法,选择粘贴命令将粘贴到文本框。这确实看起来像是wpf中的一个错误,我发现很少有文章表明它已经知道,但我没有找到它的票。
Disabled ContextMenus in WPF bug
Why is my ContextMenu item disabled?
修改强>
作为您的方案的解决方法,只需添加:
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
Dispatcher.BeginInvoke(new Func<bool>(Focus));
}
致MyListView
班。