繁体   English   中英

使用MVVM将命令绑定到DataTemplate中的控件

[英]Binding commands to controls in a DataTemplate using MVVM

MVVM的新手,我试图了解如何将命令绑定到数据模板中包含的控件。 这是我的XAML当前的样子:

<UserControl>
<Grid>
    <ListBox Grid.Row="1" x:Name="listBox1" ItemsSource="{Binding MyObservableCollection}" SelectionMode="Single">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListBoxItem>
                    <ListBoxItem.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Apply"/>
                        </ContextMenu>
                    </ListBoxItem.ContextMenu>
                    <Imagin.Controls:Thumbnail Source="{Binding ImageSource}"/>
                </ListBoxItem>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="Margin" Value="1,0" />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

需要明确的是,我可以使用视图模型将可观察的集合绑定到ListBox。 我有一个看起来像这样的视图模型类:

using System;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Drawing;
using Imagin.Imaging;
using System.Collections.Generic;

namespace MyViewModelNameSpace
{
    public class MyObject
    {
        private string name = "";
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        private ImageSource imageSource = null;
        public ImageSource ImageSource
        {
            get
            {
                return imageSource;
            }
            set
            {
                imageSource = value;
            }
        }

        public MyObject()
        {

        }
        public MyObject(string Name, ImageSource ImageSource)
        {
            this.Name = Name;
            this.ImageSource = ImageSource;
        }
    }

    public class MyViewModel : ASimplerViewModel
    {
        public MyViewModel(MainWindowViewModel mainWindowViewModel)
        {
            if (mainWindowViewModel == null)
            {
                throw new ArgumentNullException("mainWindowViewModel");
            }

            this.MainWindowViewModel = mainWindowViewModel;
        }

        private MainWindowViewModel MainWindowViewModel
        {
            get;
            set;
        }

        private ObservableCollection<MyObject> myObservableCollection = new ObservableCollection<MyObject>();
        public ObservableCollection<MyObject> MyObservableCollection
        {
            get
            {
                return myObservableCollection;
            }
            set
            {
                myObservableCollection = value;
            }
        }

    }
}

我已经尝试过通过以下方式绑定命令:

<ListBox MouseDown="SomeMouseDownEvent">

但是我不知道在哪里定义事件? 我也尝试过:

<ListBox MouseDown="{Binding SomeEventInMyViewModelClass}">

但是我收到一条错误消息,说您不能以这种方式绑定事件。

如何使用MVVM适当定义命令并将其绑定到数据模板中的对象?

MouseDown是一个RoutedEvent,因此需要一个RoutedEventHandler。 只有实现ICommandSource的控件才支持命令,并且这些控件将其公开为Command属性。 您需要Windows.Interactivity.dll。它具有EventTriggers,可将Command附加到任何RoutedEvent。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM