C# 使用MVVM将命令绑定到数据模板中的控件
MVVM新手,我试图理解如何将命令绑定到数据模板中包含的控件。这就是我的XAML当前的样子:C# 使用MVVM将命令绑定到数据模板中的控件,c#,wpf,mvvm,data-binding,datatemplate,C#,Wpf,Mvvm,Data Binding,Datatemplate,MVVM新手,我试图理解如何将命令绑定到数据模板中包含的控件。这就是我的XAML当前的样子: <UserControl> <Grid> <ListBox Grid.Row="1" x:Name="listBox1" ItemsSource="{Binding MyObservableCollection}" SelectionMode="Single"> <ListBox.ItemTemplate>
<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>
明确地说,我能够使用我的视图模型将我的可观察集合绑定到列表框。我有一个视图模型类,看起来像这样:
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;
}
}
}
}
使用系统;
使用System.Collections.ObjectModel;
使用System.Windows.Media;
使用系统图;
运用影像学;
使用System.Collections.Generic;
名称空间MyViewModelNameSpace
{
公共类MyObject
{
私有字符串名称=”;
公共字符串名
{
得到
{
返回名称;
}
设置
{
名称=值;
}
}
私有ImageSource ImageSource=null;
公共图像源图像源
{
得到
{
返回图像源;
}
设置
{
imageSource=值;
}
}
公共对象()
{
}
公共MyObject(字符串名称,ImageSource ImageSource)
{
this.Name=Name;
this.ImageSource=ImageSource;
}
}
公共类MyViewModel:ASimplerViewModel
{
公共MyViewModel(MainWindowViewModel MainWindowViewModel)
{
如果(mainWindowViewModel==null)
{
抛出新ArgumentNullException(“mainWindowViewModel”);
}
this.MainWindowViewModel=MainWindowViewModel;
}
专用MainWindowViewModel MainWindowViewModel
{
得到;
设置
}
私有ObservableCollection myObservableCollection=新ObservableCollection();
公共可观测集合MyObservableCollection
{
得到
{
返回myObservableCollection;
}
设置
{
myObservableCollection=值;
}
}
}
}
我尝试过以这种方式绑定命令:
<ListBox MouseDown="SomeMouseDownEvent">
但我不知道该在哪里定义这个事件?我也试过:
<ListBox MouseDown="{Binding SomeEventInMyViewModelClass}">
但是我得到一个错误,你不能这样绑定事件
如何使用MVVM适当地定义命令并将其绑定到数据模板中的对象?MouseDown是RoutedEvent,因此它需要RoutedEventHandler。 只有那些实现ICommandSource的控件才支持命令,并且这些控件将其作为命令属性公开。
您需要Windows.Interactivity.dll。它具有事件触发器,可帮助将命令附加到任何RoutedEvent。MouseDown是RoutedEvent,因此它需要RoutedEventHandler。 只有那些实现ICommandSource的控件才支持命令,并且这些控件将其作为命令属性公开。
您需要Windows.Interactivity.dll。它有EventTriggers,可帮助将命令附加到任何RoutedEvent。在codebehind中。xaml.cs您不能从MyViewModel类绑定它?我必须能够从MainWindowViewModel类访问成员,该类链接到MyViewModel类。我可以在数据模板中访问MyViewModel成员,但不能在xaml.cs中访问。您可以在这里找到答案:)在codebehind中。xaml.cs您不能从MyViewModel类绑定它?我必须能够从MainWindowViewModel类访问成员,该类链接到MyViewModel类。我可以在数据模板中访问MyViewModel成员,但不能在xaml.cs中访问。您可以在这里找到答案:)实际上,我设法找到了一种更简单的方法,向ItemContainerStyle添加了一个EventSetter。这是因为它同样有效。谢谢实际上,我设法找到了一种更简单的方法,在ItemContainerStyle中添加了一个EventSetter。这是因为它同样有效。谢谢