【问题标题】:Attached Behavior to execute command for ListViewItem为 ListViewItem 执行命令的附加行为
【发布时间】: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;

标签: c# wpf mvvm


【解决方案1】:

问题在于BindingDataContext 是字符串。由于字符串类没有Foo 属性,因此您会收到错误消息。这在其他情况下不会发生,因为它们从父级继承了 DataContext(对于自动生成的数据项容器不会发生这种情况 - 它们的 DataContext 是数据项)。

如果您更改绑定以使用父 ListViewDataContext,它应该可以正常工作:

Value="{Binding DataContext.Foo, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"

【讨论】:

  • 感谢您的快速答复,Abe。你是个明星,你想弄清楚这个问题已经太久了。
  • 没问题,保罗。绑定错误真的很神秘,有时只需要一双新的眼睛就能看到发生了什么!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
相关资源
最近更新 更多