使用 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 上。我有一个视图模型 class,看起来像这样:
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 的控件才支持命令,并且这些控件将其公开为命令 属性。
您需要 Windows.Interactivity.dll。它具有 EventTriggers,有助于将命令附加到任何 RoutedEvent。
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 上。我有一个视图模型 class,看起来像这样:
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 的控件才支持命令,并且这些控件将其公开为命令 属性。 您需要 Windows.Interactivity.dll。它具有 EventTriggers,有助于将命令附加到任何 RoutedEvent。