ItemsControl中的MVVM Light命令 [英] MVVM Light Commands within an ItemsControl
问题描述
我只是使用MVVM Light框架来尝试WP7开发人员.
I'm just trying my hand at WP7 dev using the MVVM Light framework.
我试图在ItemsControl中触发按钮命令,本质上是汽车列表,我希望每个元素都具有一个编辑按钮. 视图的相关部分:
I'm trying to fire a button command inside an ItemsControl, essentialy it's a list of cars and I'd like each element to have an edit button. The Relevant piece of the View:
<ItemsControl ItemsSource="{Binding MyCars}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="CarViewGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="100" />
<ColumnDefinition Width="Auto" MinWidth="302"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="40" />
<RowDefinition Height="Auto" MinHeight="32" />
<RowDefinition Height="Auto" MinHeight="32" />
<RowDefinition Height="Auto" MinHeight="32" />
</Grid.RowDefinitions>
<TextBlock x:Name="CarName" Text="{Binding Name, Mode=TwoWay}" Margin="7,0" Grid.Row="0" Grid.ColumnSpan="2" FontSize="32" FontWeight="Bold" FontStyle="Normal" />
<TextBlock x:Name="Make" Text="{Binding Make, Mode=TwoWay}" Margin="15,0" Grid.Row="1" Grid.Column="0" FontSize="24" />
<TextBlock x:Name="Model" Text="{Binding Model, Mode=TwoWay}" Grid.Row="1" Grid.Column="1" FontSize="24" />
<TextBlock x:Name="Odometer" Text="{Binding Odometer, Mode=TwoWay}" Margin="15,0" Grid.Row="2" Grid.ColumnSpan="2" FontSize="24" />
<Button x:Name="EditCarButton" Content="Edit" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right" Width="100" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding EditCar}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
我的ViewModel包含以下内容:
My ViewModel contains this:
public RelayCommand OpenNewForm { get; private set; }
public CarViewModel()
{
//Snip
EditCar = new RelayCommand<Car>(c =>
{
CurrentCar = c;
FormVisible = true;
});
}
现在您可以看到,我正在尝试传递通过CommandParameter绑定的当前Car对象.我的代表从不解雇,所以我猜我在绑定当前DataContext时出了点问题.
Now as you can see I'm trying to pass the current Car object that is bound through the CommandParameter. My delegate never fires so I'm guessing I've got something wrong in my binding regarding the current DataContext.
有人对我在做什么错有任何想法吗?
Anybody got any ideas as to what I'm doing wrong?
推荐答案
在DataTemplate中,默认情况下,DataContext设置为由DataTemplate表示的项(在这种情况下为Car对象).如果EditCar命令位于主视图模型(也包含MyCars集合)上,则需要将绑定的源显式设置为该对象.这将是(假设您正在使用MVVM Light的ViewModelLocator,并且您的VM被命名为Main){Binding Source = {StaticResource Locator},Path = Main.EditCar}
In a DataTemplate, the DataContext is set by default to the item that is represented by the DataTemplate (in that case, the Car object). If the EditCar command is on the main viewmodel (which also contains the MyCars collection), you need to explicitly set the Source of the Binding to that object. This would be (assuming that you are using the MVVM Light's ViewModelLocator and that your VM is named Main) {Binding Source={StaticResource Locator}, Path=Main.EditCar}
干杯, 洛朗(Laurent)
Cheers, Laurent
这篇关于ItemsControl中的MVVM Light命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!