- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序是 wpf mvvm,对事件使用 RelayCommand\EventToCommand 绑定(bind)。我的应用程序从 ListBox 到 ItemsControl 进行了一些典型的拖放操作(它实际上是一个顶部带有 ItemsControl 的图像控件,用于保存已放置的项目)。 ListBox 填充有 vm ObservableCollection。 ItemsControl 也是一个 ObservableCollection,我将放置的 MyObj 项目插入其中。
当我从 ListBox 拖动项目并将它们放到\on 到 ItemsControl\image 时,一切正常。在 PreviewMouseLeftButtonDownCommand 中,我使用 System.Windows.Media.VisualTreeHelper 递归地向上走可视化树,因此当我从 ListBox 拖动时,我可以找到正在拖动的 MyObj 项。但是当我尝试从 ItemsControl 中拖动一个项目时,代码不起作用。我能得到的只是项目(标签)的 DataTemplate 转换。所以我的问题是;当 PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand 触发时,如何从我的 ItemsControl 中获取所选项目?
虚拟机 C#:
PreviewMouseLeftButtonDownCommand = new RelayCommand<MouseButtonEventArgs>(e =>
{
if (e.Source is ListBox)
{
// get dragged list box item
ListBox listBox = e.Source as ListBox;
ListBoxItem listBoxItem = VisualHelper.FindAncestor<ListBoxItem>((DependencyObject)e.OriginalSource);
// Find the data behind the listBoxItem
if (listBox == null || listBoxItem == null) return;
MyObj tag = (MyObj)listBox.ItemContainerGenerator.ItemFromContainer(listBoxItem);
// Initialize the drag & drop operation
DataObject dragData = new DataObject("myObj", tag);
DragDrop.DoDragDrop(listBoxItem, dragData, DragDropEffects.Move);
}
else if (e.Source is ItemsControl)
{
ItemsControl itemsControl = e.Source as ItemsControl;
object item = VisualHelper.FindAncestor<UIElement>((DependencyObject)e.OriginalSource);
if (itemsControl == null || item == null) return;
MyObj tag = (MyObj)itemsControl.ItemContainerGenerator.ItemFromContainer(item);
// Initialize the drag & drop operation
DataObject dragData = new DataObject("myObj", tagDragging);
DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move);
}
});
最佳答案
这是我过去使用的代码:
private void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get ItemsControl for object being dragged
if (sender is ItemsControl)
_sourceItemsControl = sender as ItemsControl;
else if (sender is Panel)
_sourceItemsControl = WPFHelpers.FindAncester<ItemsControl>(sender as Panel);
// Get ItemContainer for object being dragged
FrameworkElement sourceItemsContainer = _sourceItemsControl
.ContainerFromElement((Visual)e.OriginalSource) as FrameworkElement;
// Get data object for object being dragged
if (sourceItemsContainer == null)
_draggedObject = _sourceItemsControl.DataContext;
else if (sourceItemsContainer == e.Source)
_draggedObject = e.Source;
else
_draggedObject = sourceItemsContainer.DataContext;
}
public class WPFHelpers
{
public static T FindAncester<T>(DependencyObject current)
where T : DependencyObject
{
current = VisualTreeHelper.GetParent(current);
while (current != null)
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
};
return null;
}
}
关于c# - 当 PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand 触发时,在 ItemsControl 中查找所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001607/
我使用 EventToCommand 并希望每次触发事件时更新 CommandParameter 绑定(bind)。有什么方法可以实现吗? 最佳答案 您可以像这样使用 PassEventArgsToC
我在附加事件中使用 Validation.Error的文本框。 Validation.Error 我想绑定(bind)到EventToCommand . 通常它不起作用:
你好 我通过连接到 MVVM 的 EventToCommand 获得了这个 xaml,此代码的问题是,在离开包含它的页面后,它始终保留在内存中。
我正在使用 MVVM-light-Toolkit 中的 EventToCommand 类来处理 WPF-DataGrid 中的 AutoGeneratingColumn-Event。它在我的 Main
我对 Mvvmlight 中的 RelayCommand 和 EventToCommand 有点困惑。似乎 EventToCommand 处理 EventTrigger 并调用 RelayComman
我是 SilverLight 和 Mvvm-Light 的新手。我的 View 上有一个 DataForm,它显示/编辑我的 View 模型的 SelectedPerson 属性(一个 Person
我在 Windows Phone 8 项目中使用 GalaSoft - MVVM Light Toolkit 时遇到了一个相当奇怪的问题。突然(在合并一些东西之后)我所有的 EventToComman
大家好,我正在尝试在列表框的项目中实现点击效果,但我一直收到此错误: 未找到类型“cmd:EventToCommand”。确认您没有缺少程序集引用,并且所有引用的程序集都已构建。 以及我尝试实现点击
我有以下 XAML 代码,每次按下回车按钮时用户都会在其中提交内容。 EventToCommand 调用 ViewModel 中的 AddFieldCommand 方法。
我使用 Mvvm light(wpf45)。 我想在 XAML 中添加 EventToCommand,我使用这个 xmlns:cmd ="http://www.galasoft.ch/mvvmligh
我正在为 WP8 开发一个应用程序,我想与 Windows 商店版本共享一些 ViewModel,所以我创建了一个可移植库,安装了 MvvmLight 的可移植版本并将代码移到那里。 据我所知,如果我
我有一个 StackPanel,里面有一个 Listview。 我希望能够在窗口内滚动,以更改 selectedItem。 澄清; 我想在滚动鼠标滚轮时更改我的 ViewModels Selected
这个有用的类似乎从最新的 MVVM Light 版本中消失了,知道为什么或如何解决它吗? 我正在使用 MvvmLightLibs.5.0.1.0,肯定在 MvvmLightLibs.4.1.27.0
Android 上是否存在类似的 EventToCommand(行为)? 示例 Silverlight/WPF:
我已经阅读了论坛上为 EventToCommand 提供的每个答案,但无法触发我的事件。这是构成我的问题的代码片段。我使用的是 Visual Studio 2015 社区版。 用户控件声明: xmln
我有一个 ViewModel,其中包含一个通过 CommandManager 向其他 View 公开的命令。 现在,如果我双击 TreeView ( https://catelproject.atla
我正在使用 MVVM Light 构建 Windows Phone 8 应用程序。到目前为止,一切都很好。 但是,当我使用 EventToCommand 时,出现多个错误。一个类似的问题在这里 Eve
我在 View 中有一个带有规则的组合框,它运行良好,但我希望 itemsource 中的模型的另一个字段用于绑定(bind)(或当我使用它时)更新另一个字段分数。 例如。如果您在组合框中选择规则 1
如何以编程方式将 EventToCommand 绑定(bind)到 Windows Phone 8 中的 ApplicationBar Button 或 MenuItem?我目前正在使用 MVVM l
大家好,我是 WPF 的新手,我已经开始使用基于 mvvm light 框架的 mvvm 模式实现一个应用程序。我发现它很棒但是我在应该一起交互的控件上使用两个 EventToCommand 时遇到了
我是一名优秀的程序员,十分优秀!