【发布时间】:2011-06-05 12:25:04
【问题描述】:
当用户双击列表项时,我正在尝试使用附加行为在我的 ViewModel 中执行命令。
我已经查看了许多关于该主题的文章,并尝试创建一个简单的测试应用程序,但仍然遇到问题,例如。 Firing a double click event from a WPF ListView item using MVVM
我的简单测试 ViewModel 有 2 个集合,一个返回字符串列表,另一个返回 ListViewItem 类型列表
public class ViewModel
{
public ViewModel()
{
Stuff = new ObservableCollection<ListViewItem>
{
new ListViewItem { Content = "item 1" },
new ListViewItem { Content = "item 2" }
};
StringStuff = new ObservableCollection<string> { "item 1", "item 2" };
}
public ObservableCollection<ListViewItem> Stuff { get; set; }
public ObservableCollection<string> StringStuff { get; set; }
public ICommand Foo
{
get
{
return new DelegateCommand(this.DoSomeAction);
}
}
private void DoSomeAction()
{
MessageBox.Show("Command Triggered");
}
}
这是附加的属性,就像您看到的其他示例一样:
public class ClickBehavior
{
public static DependencyProperty DoubleClickCommandProperty = DependencyProperty.RegisterAttached("DoubleClick",
typeof(ICommand),
typeof(ClickBehavior),
new FrameworkPropertyMetadata(null, new PropertyChangedCallback(ClickBehavior.DoubleClickChanged)));
public static void SetDoubleClick(DependencyObject target, ICommand value)
{
target.SetValue(ClickBehavior.DoubleClickCommandProperty, value);
}
public static ICommand GetDoubleClick(DependencyObject target)
{
return (ICommand)target.GetValue(DoubleClickCommandProperty);
}
private static void DoubleClickChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
ListViewItem element = target as ListViewItem;
if (element != null)
{
if ((e.NewValue != null) && (e.OldValue == null))
{
element.MouseDoubleClick += element_MouseDoubleClick;
}
else if ((e.NewValue == null) && (e.OldValue != null))
{
element.MouseDoubleClick -= element_MouseDoubleClick;
}
}
}
static void element_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
UIElement element = (UIElement)sender;
ICommand command = (ICommand)element.GetValue(ClickBehavior.DoubleClickCommandProperty);
command.Execute(null);
}
}
在我的主窗口中,我定义了设置附加行为并绑定到 Foo 命令的样式
<Window.Resources>
<Style x:Key="listViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="local:ClickBehavior.DoubleClick" Value="{Binding Foo}"/>
</Style>
</Window.Resources>
在定义 ListViewItems 时工作正常:
<!-- Works -->
<Label Grid.Row="2" Content="DoubleClick click behaviour:"/>
<ListView Grid.Row="2" Grid.Column="1" ItemContainerStyle="{StaticResource listViewItemStyle}">
<ListViewItem Content="Item 3" />
<ListViewItem Content="Item 4" />
</ListView>
当绑定到 ListViewItem 类型的列表时,这也有效:
<!-- Works when items bound are of type ListViewItem -->
<Label Grid.Row="3" Content="DoubleClick when bound to ListViewItem:"/>
<ListView Grid.Row="3" Grid.Column="1" ItemContainerStyle="{StaticResource listViewItemStyle}" ItemsSource="{Binding Stuff}">
</ListView>
但这不是:
<!-- Does not work when items bound are not ListViewItem -->
<Label Grid.Row="4" Content="DoubleClick when bound to string list:"/>
<ListView Grid.Row="4" Grid.Column="1" ItemContainerStyle="{StaticResource listViewItemStyle}" ItemsSource="{Binding StringStuff}">
</ListView>
在输出窗口中您会看到错误,但很难理解哪里出了问题。
System.Windows.Data 错误:39:BindingExpression 路径错误:在 'object 上找不到 'Foo' 属性'''字符串' (HashCode=785742638)'。绑定表达式:路径=Foo; DataItem='String' (HashCode=785742638);目标元素是'ListViewItem'(名称='');目标属性是“DoubleClick”(输入“ICommand”)
所以我的问题是:当您将 ListView 绑定到模型对象列表时,如何将 Command 正确连接到每个 ListViewItem?
谢谢。
【问题讨论】:
-
这非常有用!为了增加可重用性,我将 ListViewItem element = target 更改为 ListViewItem; to Control element = target as Control;